Last active
September 5, 2025 15:05
-
-
Save qntm/7905d07f71feef6d8579320d73df536b to your computer and use it in GitHub Desktop.
Effective user age verification without handling photographs, credit card numbers, government ID etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timersPromises from 'node:timers/promises' | |
const FEB = 1 | |
const MAR = 2 | |
// `setTimeout` disallows numbers which don't fit into a signed 32-bit integer. | |
// In other words | |
const MAX_DELAY = 2 ** 31 - 1 | |
const add18Years = date => { | |
date = new Date(date) | |
// Use local time. | |
// If it's 29 February, skip forward to midnight on 1 March before adding 18 years | |
if ( | |
date.getMonth() === FEB && | |
date.getDate() === 29 | |
) { | |
date.setMonth(MAR) | |
date.setDate(1) | |
date.setHours(0) | |
date.setMinutes(0) | |
date.setSeconds(0) | |
date.setMilliseconds(0) | |
} | |
date.setFullYear(date.getFullYear() + 18) | |
return date | |
} | |
const waitUntil = async date => { | |
let remaining | |
while ((remaining = date.getTime() - Date.now()) > MAX_DELAY) { | |
await timersPromises.setTimeout(MAX_DELAY) | |
} | |
await timersPromises.setTimeout(remaining) | |
} | |
export const verifyAge = async () => { | |
await waitUntil(add18Years(new Date())) | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: