Stirling-Tools/Stirling-PDF · error · Error

Timed out waiting for anonymous session token

Error message

Timed out waiting for anonymous session token

What it means

Thrown by useAutoAnonymousAuth when signInAnonymously() succeeds but waitForToken(7000) cannot find an access_token in the Supabase session within 7 seconds. The hook polls supabase.auth.getSession() every 120ms and also listens to onAuthStateChange; if neither yields a token within the window, the auth is treated as failed. The error is caught internally and surfaced as autoAuthError state rather than propagated.

Source

Thrown at frontend/editor/src/saas/hooks/useAutoAnonymousAuth.ts:92

  const triggerAnonymousAuth = useCallback(async () => {
    if (state.isAutoAuthenticating) return;

    setState((prev) => ({
      ...prev,
      isAutoAuthenticating: true,
      autoAuthError: null,
    }));
    try {
      console.log("[useAutoAnonymousAuth] anonymous auth starting");

      const { error } = await signInAnonymously();
      if (error) throw error;

      // Wait for a usable token so first API calls won't 401/redirect
      const ok = await waitForToken(7000);
      if (!ok) {
        throw new Error("Timed out waiting for anonymous session token");
      }

      console.log("[useAutoAnonymousAuth] anonymous auth complete");
      setState((prev) => ({
        ...prev,
        isAutoAuthenticating: false,
        shouldTriggerAutoAuth: false,
      }));
    } catch (e) {
      console.error("[useAutoAnonymousAuth] anonymous auth failed", e);
      setState((prev) => ({
        ...prev,
        isAutoAuthenticating: false,
        autoAuthError:
          e instanceof Error ? e.message : "Anonymous authentication failed",
      }));
    }
  }, [state.isAutoAuthenticating, waitForToken]);

View on GitHub (pinned to 9ef20dcab8)

Solutions

  1. Increase the waitForToken timeout beyond 7000ms to accommodate high-latency regions
  2. Verify the Supabase project region is close to the user base
  3. Check that anonymous sign-in is enabled in Supabase Dashboard > Authentication > Sign In Providers
  4. Test in a clean browser profile without extensions to rule out ad-blocker interference
  5. Verify localStorage is accessible (open DevTools > Application > Local Storage)

Example fix

// before
const ok = await waitForToken(7000);

// after
const ok = await waitForToken(12000); // accommodate high-latency regions
Defensive patterns

Strategy: retry

Validate before calling

// Check localStorage availability before relying on anonymous auth
function isStorageAvailable(): boolean {
  try {
    const k = '__test__';
    localStorage.setItem(k, '1');
    localStorage.removeItem(k);
    return true;
  } catch {
    return false;
  }
}
if (!isStorageAvailable()) {
  // Fall back to a non-anonymous prompt or show an error
}

Try / catch

// The hook already catches internally; expose autoAuthError to the UI:
const { autoAuthError, isAutoAuthenticating } = useAutoAnonymousAuth();
useEffect(() => {
  if (autoAuthError) {
    showAuthErrorBanner(autoAuthError);
  }
}, [autoAuthError]);

Prevention

When it happens

Trigger: signInAnonymously() returns no error but the session isn't persisted to localStorage fast enough; the Supabase project is in a geographically distant region adding latency; the browser has localStorage disabled (privacy mode, strict cookie policy); an ad-blocker or extension interferes with the Supabase JS client's network/storage.

Common situations: Supabase project hosted in a different region than the user (high RTT); browser in private/incognito mode with storage restrictions; ad-blocker or privacy extension blocking Supabase API calls; Supabase project under rate-limiting or temporary degraded service.

Understand the failure class

Related errors


AI-assisted analysis of Stirling-Tools/Stirling-PDF@9ef20dcab8 (2026-08-13). Data as JSON: /api/errors/588719cf9713142c. Report an issue: GitHub.