mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 09:51:13 -05:00
Fix avatar loading by preventing profile state race condition
This commit is contained in:
@@ -97,7 +97,7 @@ export function AuthButtons() {
|
|||||||
return <DropdownMenu>
|
return <DropdownMenu>
|
||||||
<DropdownMenuTrigger asChild>
|
<DropdownMenuTrigger asChild>
|
||||||
<Button variant="ghost" className="relative h-8 w-8 rounded-full">
|
<Button variant="ghost" className="relative h-8 w-8 rounded-full">
|
||||||
<Avatar className="h-8 w-8">
|
<Avatar className="h-8 w-8" key={profile?.avatar_url || 'fallback'}>
|
||||||
<AvatarImage
|
<AvatarImage
|
||||||
src={profile?.avatar_url || undefined}
|
src={profile?.avatar_url || undefined}
|
||||||
alt={profile?.display_name || profile?.username || user.email || 'User avatar'}
|
alt={profile?.display_name || profile?.username || user.email || 'User avatar'}
|
||||||
|
|||||||
@@ -44,26 +44,22 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
|
|||||||
|
|
||||||
if (error && error.code !== 'PGRST116') {
|
if (error && error.code !== 'PGRST116') {
|
||||||
authError('[Auth] Error fetching profile:', error);
|
authError('[Auth] Error fetching profile:', error);
|
||||||
if (isMountedRef.current) {
|
// Don't clear profile on error - keep existing data
|
||||||
setProfile(null);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isMountedRef.current) {
|
if (isMountedRef.current && data) {
|
||||||
console.log('[Auth] ✅ Profile loaded:', {
|
console.log('[Auth] ✅ Profile loaded:', {
|
||||||
userId: data?.user_id,
|
userId: data.user_id,
|
||||||
username: data?.username,
|
username: data.username,
|
||||||
avatar_url: data?.avatar_url,
|
avatar_url: data.avatar_url,
|
||||||
hasData: !!data
|
hasData: !!data
|
||||||
});
|
});
|
||||||
setProfile(data as Profile);
|
setProfile(data as Profile);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
authError('[Auth] Error fetching profile:', error);
|
authError('[Auth] Error fetching profile:', error);
|
||||||
if (isMountedRef.current) {
|
// Don't clear profile on error - keep existing data
|
||||||
setProfile(null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -220,17 +216,18 @@ function AuthProviderComponent({ children }: { children: React.ReactNode }) {
|
|||||||
|
|
||||||
// Handle profile fetching for authenticated users (async, doesn't block loading)
|
// Handle profile fetching for authenticated users (async, doesn't block loading)
|
||||||
if (session?.user) {
|
if (session?.user) {
|
||||||
if (profileFetchTimeoutRef.current) {
|
// Only fetch if we don't have a profile or user ID changed
|
||||||
clearTimeout(profileFetchTimeoutRef.current);
|
if (!profile || profile.user_id !== session.user.id) {
|
||||||
}
|
if (profileFetchTimeoutRef.current) {
|
||||||
|
clearTimeout(profileFetchTimeoutRef.current);
|
||||||
|
profileFetchTimeoutRef.current = null;
|
||||||
|
}
|
||||||
|
|
||||||
profileFetchTimeoutRef.current = setTimeout(() => {
|
// Call directly - fetchProfile is already async
|
||||||
fetchProfile(session.user.id);
|
fetchProfile(session.user.id);
|
||||||
profileFetchTimeoutRef.current = null;
|
}
|
||||||
}, 0);
|
|
||||||
} else {
|
|
||||||
setProfile(null);
|
|
||||||
}
|
}
|
||||||
|
// Profile is already cleared in SIGNED_OUT handler above
|
||||||
});
|
});
|
||||||
|
|
||||||
// THEN get initial session (this may trigger INITIAL_SESSION event)
|
// THEN get initial session (this may trigger INITIAL_SESSION event)
|
||||||
|
|||||||
Reference in New Issue
Block a user