games_app/backend/dist-test-faq/repositories/emailVerificationRepository.js

51 lines
1.9 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteByUserId = deleteByUserId;
exports.create = create;
exports.findValidByTokenHash = findValidByTokenHash;
exports.markUsed = markUsed;
exports.deleteStale = deleteStale;
const connection_1 = require("../db/connection");
/**
* Repository for single-use email verification tokens.
* Stores hashed tokens with expiry; marks tokens used after successful verification.
*/
/**
* Removes all verification tokens for a user (before issuing a new one).
* @param userId - User UUID
*/
async function deleteByUserId(userId) {
await (0, connection_1.query)('DELETE FROM email_verification_tokens WHERE user_id = $1', [userId]);
}
/**
* Inserts a new verification token row.
* @param userId - User UUID
* @param tokenHash - SHA-256 hash of the raw token
* @param expiresAt - Expiration timestamp
*/
async function create(userId, tokenHash, expiresAt) {
await (0, connection_1.query)('INSERT INTO email_verification_tokens (user_id, token_hash, expires_at) VALUES ($1, $2, $3)', [userId, tokenHash, expiresAt]);
}
/**
* Finds a valid unused token by hash.
* @param tokenHash - SHA-256 hash of the raw token from the verify link
*/
async function findValidByTokenHash(tokenHash) {
const rows = await (0, connection_1.query)(`SELECT id, user_id FROM email_verification_tokens
WHERE token_hash = $1 AND expires_at > NOW() AND used_at IS NULL`, [tokenHash]);
return rows[0] || null;
}
/**
* Marks a token as used so it cannot be replayed.
* @param id - Token row UUID
*/
async function markUsed(id) {
await (0, connection_1.query)('UPDATE email_verification_tokens SET used_at = NOW() WHERE id = $1', [id]);
}
/**
* Deletes expired or used tokens (housekeeping).
*/
async function deleteStale() {
await (0, connection_1.query)('DELETE FROM email_verification_tokens WHERE expires_at <= NOW() OR used_at IS NOT NULL');
}
//# sourceMappingURL=emailVerificationRepository.js.map