diff --git a/apps/ui/src/lib/http-api-client.ts b/apps/ui/src/lib/http-api-client.ts index f069af1c..2943f3e2 100644 --- a/apps/ui/src/lib/http-api-client.ts +++ b/apps/ui/src/lib/http-api-client.ts @@ -179,8 +179,24 @@ let apiKeyInitialized = false; let apiKeyInitPromise: Promise | null = null; // Cached session token for authentication (Web mode - explicit header auth) -// Only used in-memory after fresh login; on refresh we rely on HTTP-only cookies +// Persisted to localStorage to survive page reloads let cachedSessionToken: string | null = null; +const SESSION_TOKEN_KEY = 'automaker:sessionToken'; + +// Initialize cached session token from localStorage on module load +// This ensures web mode survives page reloads with valid authentication +const initSessionToken = (): void => { + if (typeof window === 'undefined') return; // Skip in SSR + try { + cachedSessionToken = window.localStorage.getItem(SESSION_TOKEN_KEY); + } catch { + // localStorage might be disabled or unavailable + cachedSessionToken = null; + } +}; + +// Initialize on module load +initSessionToken(); // Get API key for Electron mode (returns cached value after initialization) // Exported for use in WebSocket connections that need auth @@ -200,14 +216,30 @@ export const waitForApiKeyInit = (): Promise => { // Get session token for Web mode (returns cached value after login) export const getSessionToken = (): string | null => cachedSessionToken; -// Set session token (called after login) +// Set session token (called after login) - persists to localStorage for page reload survival export const setSessionToken = (token: string | null): void => { cachedSessionToken = token; + if (typeof window === 'undefined') return; // Skip in SSR + try { + if (token) { + window.localStorage.setItem(SESSION_TOKEN_KEY, token); + } else { + window.localStorage.removeItem(SESSION_TOKEN_KEY); + } + } catch { + // localStorage might be disabled; continue with in-memory cache + } }; // Clear session token (called on logout) export const clearSessionToken = (): void => { cachedSessionToken = null; + if (typeof window === 'undefined') return; // Skip in SSR + try { + window.localStorage.removeItem(SESSION_TOKEN_KEY); + } catch { + // localStorage might be disabled + } }; /**