Fix: Ensure loading state is set correctly for session events in AuthProvider

This commit is contained in:
pacnpal
2025-10-12 11:31:01 -04:00
parent 0443e96c4a
commit e3331dae96
2 changed files with 18 additions and 12 deletions

View File

@@ -189,6 +189,7 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
setUser(null);
setProfile(null);
sessionVerifiedRef.current = false;
// CRITICAL: Set loading to false immediately when no session
setLoading(false);
}
} else if (event === 'SIGNED_OUT') {
@@ -292,7 +293,7 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
} else {
if (isMountedRef.current) {
setProfile(null);
// If no session and this is an init event, resolve loading immediately
// CRITICAL: Always resolve loading when there's no session
if (event === 'INITIAL_SESSION' || event === 'SIGNED_OUT') {
console.log('[Auth] No session for event:', event, '- setting loading to false');
setLoading(false);
@@ -300,11 +301,11 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
}
}
// Only set loading false immediately for events that don't need profile fetch
if (event !== 'SIGNED_IN' && event !== 'INITIAL_SESSION') {
console.log('[Auth] Setting loading to false immediately for event:', event);
setLoading(false);
}
// CRITICAL: Always set loading false for non-session events
if (event !== 'SIGNED_IN' && event !== 'INITIAL_SESSION') {
console.log('[Auth] Setting loading to false immediately for event:', event);
setLoading(false);
}
});
// THEN get initial session
@@ -417,4 +418,4 @@ export function useAuth() {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
}
}