Files
wehub-resource-sync 680cc63b3d
Relay Deploy Dev / Deploy Relay Dev (push) Failing after 1s
Test / changes (push) Has been skipped
Test / backend-remote-checks (push) Waiting to run
Test / backend-clippy (push) Waiting to run
Test / backend-test (push) Waiting to run
Test / tauri-checks (push) Waiting to run
Remote Deploy Dev / Deploy Remote Dev (push) Failing after 2s
Test / frontend-checks (push) Waiting to run
Test / backend-schema-checks (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 12:12:58 +08:00

27 lines
677 B
TypeScript

import { jwtDecode } from 'jwt-decode';
type AccessTokenClaims = {
exp: number;
aud: string;
};
const TOKEN_REFRESH_LEEWAY_MS = 20_000;
const ACCESS_TOKEN_AUD = 'access';
const getTokenExpiryMs = (token: string): number | null => {
try {
const { exp, aud } = jwtDecode<AccessTokenClaims>(token);
if (aud !== ACCESS_TOKEN_AUD) return null;
if (!Number.isFinite(exp)) return null;
return exp * 1000;
} catch {
return null;
}
};
export const shouldRefreshAccessToken = (token: string): boolean => {
const expiresAt = getTokenExpiryMs(token);
if (expiresAt === null) return true;
return expiresAt - Date.now() <= TOKEN_REFRESH_LEEWAY_MS;
};