All files / src utils.ts

83.33% Statements 5/6
50% Branches 1/2
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19                          6x 45x 45x 45x 45x  
/**
 * Extracts the expiration time from a JWT token and give the token expiry time.
 * 
 * @export
 * @param {string} token - The JWT token to extract the expiration time from
 * @return {any} The expiration time value from the token's payload
 * @throws {Error} Throws an error if the exp field is missing in the token
 * 
 * @example
 * const jwtToken = 'your-token';
 * const expiryTime = findExpiryTime(jwtToken);
 * console.log(new Date(expiryTime * 1000).toISOString()); // Convert to date if it's a Unix timestamp
 */
export function findExpiryTime(token: string): any {
  const payload = token.split('.')[1];
  const decoded = JSON.parse(Buffer.from(payload, 'base64').toString('utf8'));
  Iif (!decoded.exp) throw new Error('exp field is missing in the token');
  return decoded.exp;
}