Add error handling for Supabase auth initialization to handle CORS errors gracefully

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 <noreply@anthropic.com>
This commit is contained in:
dwindown
2026-02-02 13:22:49 +07:00
parent d47be3aca6
commit f3117308c3

View File

@@ -42,6 +42,10 @@ export function AuthProvider({ children }: { children: ReactNode }) {
// No session, set loading to false immediately // No session, set loading to false immediately
if (mounted) setLoading(false); 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 // Then listen for auth state changes