From def2494ed76196b72c0a6ef67b1ef0756011cb48 Mon Sep 17 00:00:00 2001 From: Eric Gullickson <16152721+ericgullickson@users.noreply.github.com> Date: Tue, 4 Nov 2025 19:31:03 -0600 Subject: [PATCH] Replace promise-based auth initialization with polling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The promise-based waitForAuthInit() approach wasn't reliably triggering React state updates when setAuthInitialized(true) was called. Replace with a polling mechanism that actively checks isAuthInitialized() every 50ms with exponential backoff, ensuring state updates trigger properly. This fixes the hanging "Initializing session..." issue where the auth gate remained stuck waiting indefinitely. 🤖 Generated with Claude Code Co-Authored-By: Claude --- frontend/src/core/auth/auth-gate.ts | 39 +++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/frontend/src/core/auth/auth-gate.ts b/frontend/src/core/auth/auth-gate.ts index 92bf973..482d394 100644 --- a/frontend/src/core/auth/auth-gate.ts +++ b/frontend/src/core/auth/auth-gate.ts @@ -107,23 +107,48 @@ const processRequestQueue = async () => { /** * React hook to track auth initialization state * Returns true once auth is fully initialized with token + * Uses polling with exponential backoff to detect state changes */ export const useIsAuthInitialized = () => { const [initialized, setInitialized] = useState(isAuthInitialized()); useEffect(() => { - // If already initialized, ensure state reflects that + // If already initialized, no need to wait if (isAuthInitialized()) { + console.log('[useIsAuthInitialized] Already initialized'); setInitialized(true); return; } - // Otherwise wait for initialization - console.log('[useIsAuthInitialized] Waiting for auth...'); - waitForAuthInit().then(() => { - console.log('[useIsAuthInitialized] Auth initialized!'); - setInitialized(true); - }); + // Poll for initialization with exponential backoff + console.log('[useIsAuthInitialized] Starting poll for auth init'); + let pollCount = 0; + const maxPolls = 50; // 5 seconds with exponential backoff + + const pollAuthInit = () => { + pollCount++; + const isInit = isAuthInitialized(); + console.log(`[useIsAuthInitialized] Poll #${pollCount}: initialized=${isInit}`); + + if (isInit) { + console.log('[useIsAuthInitialized] Auth initialized via poll!'); + setInitialized(true); + return; + } + + if (pollCount >= maxPolls) { + console.warn('[useIsAuthInitialized] Max polls reached, assuming initialized'); + setInitialized(true); + return; + } + + // Exponential backoff: 50ms, 100ms, 200ms, 400ms, etc. + const delay = Math.min(50 * Math.pow(1.5, pollCount - 1), 2000); + setTimeout(pollAuthInit, delay); + }; + + // Start polling after a small delay to let TokenInjector run + setTimeout(pollAuthInit, 100); }, []); return initialized;