All files / src/infrastructure/security revoke-refresh-token.ts

100% Statements 7/7
75% Branches 3/4
100% Functions 1/1
100% Lines 7/7

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 20 21          4x 1x     3x 3x 3x   3x 3x            
import { tokenService } from './token-service.instance';
import { tokenRevocationService } from './token-revocation-service.instance';
 
/** Best-effort revocation used on logout/account deletion — a missing or already-invalid token is a no-op. */
export async function revokeRefreshTokenIfPresent(refreshToken: string | undefined): Promise<void> {
  if (!refreshToken) {
    return;
  }
 
  try {
    const payload = tokenService.verifyRefreshToken(refreshToken);
    const remainingTtlSeconds = payload.exp - Math.floor(Date.now() / 1000);
 
    Eif (remainingTtlSeconds > 0) {
      await tokenRevocationService.revoke(payload.jti, remainingTtlSeconds);
    }
  } catch {
    // Already invalid/expired — nothing to revoke.
  }
}