feat: sessão #3 — lib (db/auth/email/validations), API routes, NextAuth v5, middleware, páginas account/shelters/shelter-dashboard, Prisma v7 fix
This commit is contained in:
35
lib/auth/age-validation.ts
Normal file
35
lib/auth/age-validation.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Validação de +18 anos — sempre executada no servidor.
|
||||
* Nunca confiar no cliente para esta verificação.
|
||||
*/
|
||||
export function isAdult(birthdate: Date): boolean {
|
||||
const today = new Date();
|
||||
const age18 = new Date(
|
||||
birthdate.getFullYear() + 18,
|
||||
birthdate.getMonth(),
|
||||
birthdate.getDate()
|
||||
);
|
||||
return today >= age18;
|
||||
}
|
||||
|
||||
export function validateAge(birthdateStr: string): {
|
||||
valid: boolean;
|
||||
error?: string;
|
||||
} {
|
||||
const birthdate = new Date(birthdateStr);
|
||||
|
||||
if (isNaN(birthdate.getTime())) {
|
||||
return { valid: false, error: 'Data de nascimento inválida.' };
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
if (birthdate > now) {
|
||||
return { valid: false, error: 'Data de nascimento não pode ser no futuro.' };
|
||||
}
|
||||
|
||||
if (!isAdult(birthdate)) {
|
||||
return { valid: false, error: 'Tens de ter pelo menos 18 anos para criar conta.' };
|
||||
}
|
||||
|
||||
return { valid: true };
|
||||
}
|
||||
Reference in New Issue
Block a user