mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 11:31:11 -05:00
Fix: Resolve perpetual loading state
This commit is contained in:
@@ -35,6 +35,7 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
|
||||
const novuUpdateTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||
const previousEmailRef = useRef<string | null>(null);
|
||||
const loadingTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||
const loadingStateRef = useRef(loading);
|
||||
|
||||
const fetchProfile = async (userId: string, retryCount = 0, onComplete?: () => void) => {
|
||||
try {
|
||||
@@ -153,6 +154,12 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
};
|
||||
|
||||
// Keep loading state ref in sync
|
||||
useEffect(() => {
|
||||
loadingStateRef.current = loading;
|
||||
console.log('[Auth] Loading state changed:', loading);
|
||||
}, [loading]);
|
||||
|
||||
useEffect(() => {
|
||||
// CRITICAL: Set up listener FIRST to catch all events
|
||||
const {
|
||||
@@ -168,12 +175,17 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
|
||||
// Clear any error
|
||||
setSessionError(null);
|
||||
|
||||
// Explicitly handle SIGNED_IN event for iframe compatibility
|
||||
// Explicitly handle SIGNED_IN and INITIAL_SESSION events
|
||||
if (event === 'SIGNED_IN' && session) {
|
||||
console.log('[Auth] SIGNED_IN detected, setting session and user');
|
||||
setSession(session);
|
||||
setUser(session.user);
|
||||
sessionVerifiedRef.current = true;
|
||||
} else if (event === 'INITIAL_SESSION' && session) {
|
||||
console.log('[Auth] INITIAL_SESSION detected, setting session and user');
|
||||
setSession(session);
|
||||
setUser(session.user);
|
||||
sessionVerifiedRef.current = true;
|
||||
} else if (event === 'SIGNED_OUT') {
|
||||
console.log('[Auth] SIGNED_OUT detected, clearing session');
|
||||
setSession(null);
|
||||
@@ -260,11 +272,13 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
|
||||
// Defer profile fetch to avoid deadlock
|
||||
const shouldWaitForProfile = event === 'SIGNED_IN';
|
||||
const shouldWaitForProfile = event === 'SIGNED_IN' || event === 'INITIAL_SESSION';
|
||||
console.log('[Auth] Profile fetch deferred, shouldWaitForProfile:', shouldWaitForProfile, 'event:', event);
|
||||
profileFetchTimeoutRef.current = setTimeout(() => {
|
||||
if (!isMountedRef.current) return;
|
||||
fetchProfile(session.user.id, 0, () => {
|
||||
if (isMountedRef.current && shouldWaitForProfile) {
|
||||
console.log('[Auth] Profile fetch complete, setting loading to false');
|
||||
setLoading(false);
|
||||
}
|
||||
});
|
||||
@@ -276,10 +290,11 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
}
|
||||
|
||||
// Only set loading false immediately for non-SIGNED_IN events
|
||||
if (event !== 'SIGNED_IN') {
|
||||
setLoading(false);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
});
|
||||
|
||||
// THEN get initial session
|
||||
@@ -311,8 +326,8 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
|
||||
|
||||
// Add a safety timeout to force loading to resolve
|
||||
loadingTimeoutRef.current = setTimeout(() => {
|
||||
if (isMountedRef.current && loading) {
|
||||
console.warn('[Auth] Loading timeout reached, forcing loading to false');
|
||||
if (isMountedRef.current && loadingStateRef.current) {
|
||||
console.warn('[Auth] Loading timeout reached after 5 seconds, forcing loading to false');
|
||||
setLoading(false);
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
Reference in New Issue
Block a user