Fix auth state updates when Supabase auth falls back to mock by subscribing to both real and mock auth state changes

cgen-8e8e553dd55a470b8f18a9db949b0a5b
This commit is contained in:
Builder.io 2025-09-30 04:37:27 +00:00
parent d7dc308cb6
commit 86db6bac9e

View file

@ -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;
},
};
}