Current access token or null if not authenticated
// Before making an API request
const token = getAccessToken();
if (token) {
headers.Authorization = `Bearer ${token}`;
} else {
// Handle unauthenticated state
}
export function getAccessToken(): string | null {
const accessToken = getAccessTokenState();
if (accessToken) return accessToken;
// 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.accessToken;
}
return null;
}
Gets the current access token
Retrieves the access token using a two-tier strategy:
This approach balances performance (memory access) with reliability (persistent storage fallback), ensuring token availability even after application restarts.