Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 5x 8x 8x 8x 8x 8x 8x 8x 8x 8x 5x 5x 5x 3x 3x 3x 3x 3x 8x | import { z } from 'zod';
export const ageGate = (ageGateType, ageGateValue, legalAge = null) => {
const ageGateTypeValidation = z.enum([ 'yesno', 'dob' ]);
const ageDateValidation = z.coerce.date();
legalAge = legalAge || 21;
let _type = null;
try {
_type = ageGateTypeValidation.parse(ageGateType.toLowerCase());
} catch {
return {};
}
let _passed = false;
let { success, data: userDOB } = ageDateValidation.safeParse(ageGateValue);
switch (_type) {
case 'yesno':
_passed = !!ageGateValue;
userDOB = null;
break;
case 'dob':
Eif (success) {
const today = new Date();
const legalAsOf = new Date(userDOB.getFullYear() + legalAge, userDOB.getMonth(), userDOB.getDate());
_passed = !legalAsOf ? false : legalAsOf <= today;
}
break;
}
return { ageGateType: _type, ageGatePassed: _passed, ageGateDOB: userDOB || null };
};
|