games_app/backend/dist/repositories/refreshTokenRepository.js
2026-06-29 15:01:23 +02:00

46 lines
1.9 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.create = create;
exports.findByTokenHash = findByTokenHash;
exports.deleteByUserId = deleteByUserId;
exports.deleteExpired = deleteExpired;
const connection_1 = require("../db/connection");
/**
* Repository for refresh token storage and lifecycle management.
* Handles creation, lookup, and cleanup of JWT refresh tokens.
*/
/**
* Stores a new refresh token for a user.
* @param userId - ID of the user who owns the token.
* @param tokenHash - Hashed token value (never store the raw token).
* @param expiresAt - Expiration timestamp for the token.
* @returns Resolves when the row has been inserted.
*/
async function create(userId, tokenHash, expiresAt) {
await (0, connection_1.query)('INSERT INTO refresh_tokens (user_id, token_hash, expires_at) VALUES ($1, $2, $3)', [userId, tokenHash, expiresAt]);
}
/**
* Finds a non-expired refresh token by its hash.
* @param tokenHash - Hashed token value to look up.
* @returns The owning user ID, or null if the token is missing or expired.
*/
async function findByTokenHash(tokenHash) {
const rows = await (0, connection_1.query)('SELECT user_id FROM refresh_tokens WHERE token_hash = $1 AND expires_at > NOW()', [tokenHash]);
return rows[0] || null;
}
/**
* Deletes all refresh tokens for a user (e.g. on logout or password change).
* @param userId - ID of the user whose tokens should be removed.
* @returns Resolves when matching rows have been deleted.
*/
async function deleteByUserId(userId) {
await (0, connection_1.query)('DELETE FROM refresh_tokens WHERE user_id = $1', [userId]);
}
/**
* Removes all refresh tokens that have passed their expiration time.
* @returns Resolves when expired rows have been deleted.
*/
async function deleteExpired() {
await (0, connection_1.query)('DELETE FROM refresh_tokens WHERE expires_at <= NOW()');
}
//# sourceMappingURL=refreshTokenRepository.js.map