Skip to content

Instantly share code, notes, and snippets.

@qntm
Last active September 5, 2025 15:05
Show Gist options
  • Save qntm/7905d07f71feef6d8579320d73df536b to your computer and use it in GitHub Desktop.
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.
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
}
@qntm
Copy link
Author

qntm commented Sep 3, 2025

Usage:

import { verifyAge } from './verify.js'
const userIsOver18 = await verifyAge()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment