From f3117308c365a286988f3b4ee3631d950ec73930 Mon Sep 17 00:00:00 2001 From: dwindown Date: Mon, 2 Feb 2026 13:22:49 +0700 Subject: [PATCH] Add error handling for Supabase auth initialization to handle CORS errors gracefully MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a user visits the site while logged out, Supabase may still try to refresh stale tokens from localStorage. If CORS is not properly configured, this causes the error shown in console and the app to get stuck. Added error handling to catch these initialization errors and set loading to false so the app can load properly even when auth refresh fails. Note: The proper fix is to add the origin to Supabase CORS settings: https://supabase.com/dashboard/project/lovable-backoffice/settings/api This just prevents the app from hanging when such errors occur. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- src/hooks/useAuth.tsx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/hooks/useAuth.tsx b/src/hooks/useAuth.tsx index 4a005e0..0ae0854 100644 --- a/src/hooks/useAuth.tsx +++ b/src/hooks/useAuth.tsx @@ -42,6 +42,10 @@ export function AuthProvider({ children }: { children: ReactNode }) { // No session, set loading to false immediately if (mounted) setLoading(false); } + }).catch((error: Error | unknown) => { + // Catch CORS errors or other initialization errors + console.error('Auth initialization error:', error); + if (mounted) setLoading(false); }); // Then listen for auth state changes