games_app/backend/src/repositories/passwordResetRepository.ts
2026-06-29 15:01:23 +02:00

34 lines
1.1 KiB
TypeScript

import { query } from '../db/connection';
export async function deleteByUserId(userId: string): Promise<void> {
await query('DELETE FROM password_reset_tokens WHERE user_id = $1', [userId]);
}
export async function create(userId: string, tokenHash: string, expiresAt: Date): Promise<void> {
await query(
'INSERT INTO password_reset_tokens (user_id, token_hash, expires_at) VALUES ($1, $2, $3)',
[userId, tokenHash, expiresAt]
);
}
export async function findValidByTokenHash(
tokenHash: string
): Promise<{ id: string; user_id: string } | null> {
const rows = await query<{ id: string; user_id: string }[]>(
`SELECT id, user_id FROM password_reset_tokens
WHERE token_hash = $1 AND expires_at > NOW() AND used_at IS NULL`,
[tokenHash]
);
return rows[0] ?? null;
}
export async function markUsed(id: string): Promise<void> {
await query('UPDATE password_reset_tokens SET used_at = NOW() WHERE id = $1', [id]);
}
export async function deleteStale(): Promise<void> {
await query(
'DELETE FROM password_reset_tokens WHERE expires_at <= NOW() OR used_at IS NOT NULL'
);
}