Timestamp when the token expires (milliseconds since epoch) or null
// Check if token needs refresh
const expiryTime = getTokenExpiry();
if (expiryTime) {
const timeRemaining = expiryTime - Date.now();
if (timeRemaining < 300000) { // Less than 5 minutes
// Token expires soon, refresh it
}
}
export function getTokenExpiry(): number | null {
const expiry = getTokenExpiryState();
if (expiry) return expiry;
// If not in memory, try loading from encrypted storage
const tokens = tokenStorage.loadTokens();
if (tokens) {
// Update memory state with loaded tokens
setAccessTokenState(tokens.accessToken);
setRefreshTokenState(tokens.refreshToken);
setTokenExpiryState(tokens.expiresAt);
return tokens.expiresAt;
}
return null;
}
Gets the token expiry timestamp
Retrieves the access token expiration timestamp using a two-tier strategy:
The expiry timestamp is used to determine when to refresh tokens and whether the current access token is still valid.