Add generación y expiración de tokens de sesión

This commit is contained in:
Carlos Narro
2026-05-30 19:34:07 +02:00
parent 49b5910593
commit 4e4cc8545e
2 changed files with 53 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
import { randomBytes, createHash } from 'node:crypto';
export const SESSION_TTL_MS = 30 * 24 * 60 * 60 * 1000; // 30 días
export function generateSessionToken(): string {
return randomBytes(32).toString('hex');
}
export function hashSessionToken(token: string): string {
return createHash('sha256').update(token).digest('hex');
}
export function isSessionExpired(expiresAt: Date, now: Date = new Date()): boolean {
return expiresAt.getTime() <= now.getTime();
}
export function sessionExpiry(now: Date = new Date()): Date {
return new Date(now.getTime() + SESSION_TTL_MS);
}