From 86db6bac9e53c27993504cd1a77a9fc853435974 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Tue, 30 Sep 2025 04:37:27 +0000 Subject: [PATCH] Fix auth state updates when Supabase auth falls back to mock by subscribing to both real and mock auth state changes cgen-8e8e553dd55a470b8f18a9db949b0a5b --- client/lib/supabase.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/client/lib/supabase.ts b/client/lib/supabase.ts index 59d1fa0c..86e8520c 100644 --- a/client/lib/supabase.ts +++ b/client/lib/supabase.ts @@ -132,14 +132,32 @@ export const supabase = new Proxy(supabaseClient || {}, { return await mockAuth.getSession(); }, onAuthStateChange: (callback: any) => { + let realSub: any = null; if (isSupabaseConfigured && target && target.auth) { try { - return target.auth.onAuthStateChange(callback); + realSub = target.auth.onAuthStateChange(callback); } catch (error) { - console.warn("Supabase onAuthStateChange failed, using mock"); + console.warn("Supabase onAuthStateChange failed, will use mock too"); } } - return mockAuth.onAuthStateChange(callback); + // Always subscribe to mock as a safety net in case we fall back during auth + const mockSub = mockAuth.onAuthStateChange(callback); + + // Return a combined unsubscribe that cleans up both + return { + data: { + subscription: { + unsubscribe: () => { + try { + realSub?.data?.subscription?.unsubscribe?.(); + } catch {} + try { + mockSub?.data?.subscription?.unsubscribe?.(); + } catch {} + }, + }, + }, + } as any; }, }; }