This commit is contained in:
rgrgogu
2026-05-06 14:15:04 +08:00
parent a8f10a25d7
commit d5fcc574a4
12 changed files with 389 additions and 219 deletions
+14 -1
View File
@@ -67,4 +67,17 @@ const verifyRefreshToken = (token) =>
const hashToken = (token) =>
crypto.createHash('sha256').update(token).digest('hex');
module.exports = { generateTokens, verifyAccessToken, verifyRefreshToken, hashToken };
/**
* SHA-256 hash a token string for safe DB storage.
* @param {string} decoded
* @param {number} thresholdDays
* @returns {string} if expired or not
*/
const shouldRotateRefreshToken = (decoded, thresholdDays = 1) => {
const now = Math.floor(Date.now() / 1000);
const timeLeft = decoded.exp - now;
const threshold = thresholdDays * 24 * 60 * 60;
return timeLeft <= threshold;
};
module.exports = { generateTokens, verifyAccessToken, verifyRefreshToken, hashToken, shouldRotateRefreshToken };