From 14a027fbb138eda2ab2adce5589c16589bfe52c1 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Wed, 5 Nov 2025 02:53:01 +0000 Subject: [PATCH] Add detailed logging to diagnose session persistence cgen-57a779f645c946618b4c383c0aef73f9 --- client/contexts/AuthContext.tsx | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/client/contexts/AuthContext.tsx b/client/contexts/AuthContext.tsx index e69189cd..ac61e2e7 100644 --- a/client/contexts/AuthContext.tsx +++ b/client/contexts/AuthContext.tsx @@ -197,25 +197,37 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ const hasAuthTokens = () => { if (typeof window === "undefined") return false; const keys = Object.keys(window.localStorage); - return keys.some( + const authKeys = keys.filter( (key) => key.includes("auth-token") || (key.includes("sb-") && key.includes("-auth")), ); + console.log("[AUTH] Auth keys in localStorage:", authKeys); + return authKeys.length > 0; }; // Get initial session with persistence recovery const initializeAuth = async () => { try { + console.log("[AUTH] Initializing auth context..."); const { data: { session }, } = await supabase.auth.getSession(); + console.log( + "[AUTH] getSession() returned:", + session?.user ? `User: ${session.user.email}` : "No session", + ); + + // Check localStorage directly + const hasTokens = hasAuthTokens(); + console.log("[AUTH] Tokens exist in storage:", hasTokens); + // If no session but tokens exist, the session might not have restored yet // Wait a bit for onAuthStateChange to trigger - if (!session && hasAuthTokens()) { + if (!session && hasTokens) { console.log( - "Tokens exist in storage but session not yet restored, waiting...", + "[AUTH] Tokens exist in storage but session not yet restored, waiting for onAuthStateChange...", ); // Don't set loading to false yet - wait for onAuthStateChange return; @@ -223,15 +235,17 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ if (session?.user) { sessionRestored = true; + console.log("[AUTH] Session restored from getSession()"); setSession(session); setUser(session.user); await fetchUserProfile(session.user.id); } else { + console.log("[AUTH] No session found, marking as initialized"); sessionRestored = true; setLoading(false); } } catch (error) { - console.error("Error getting session:", error); + console.error("[AUTH] Error getting session:", error); sessionRestored = true; setLoading(false); }