• Gets the current access token

    Retrieves the access token using a two-tier strategy:

    1. First attempts to get token from fast in-memory state
    2. Falls back to encrypted storage if not in memory
    3. When loading from storage, rehydrates memory state

    This approach balances performance (memory access) with reliability (persistent storage fallback), ensuring token availability even after application restarts.

    Returns null | string

    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;
    }