19 lines
492 B
TypeScript

import * as jwt from 'jsonwebtoken';
export function isTokenExpired(token: string, threshold: number = 300) {
try {
const decoded = jwt.decode(token);
if (!decoded || typeof decoded === 'string' || !decoded.exp) {
throw new Error("Invalid token or missing 'exp' field.");
}
const currentTime = Math.floor(Date.now() / 1_000);
const tokenExpirationTime = decoded.exp;
return tokenExpirationTime < currentTime + threshold;
} catch {
return true;
}
}