Refactor: Simplify auth and profile handling

This commit is contained in:
gpt-engineer-app[bot]
2025-10-13 17:22:56 +00:00
parent be26c08640
commit 149c0704fe
9 changed files with 78 additions and 122 deletions

50
src/hooks/useProfile.tsx Normal file
View File

@@ -0,0 +1,50 @@
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { supabase } from '@/integrations/supabase/client';
import { Profile } from '@/types/database';
export function useProfile(userId: string | undefined) {
const queryClient = useQueryClient();
const query = useQuery({
queryKey: ['profile', userId],
queryFn: async () => {
if (!userId) return null;
console.log('[useProfile] Fetching profile for userId:', userId);
const { data, error } = await supabase
.from('profiles')
.select('*, location:locations(*)')
.eq('user_id', userId)
.maybeSingle();
if (error) {
console.error('[useProfile] Error:', error);
throw error;
}
console.log('[useProfile] Profile loaded:', {
username: data?.username,
avatar_url: data?.avatar_url
});
return data as Profile;
},
enabled: !!userId,
staleTime: 5 * 60 * 1000, // 5 minutes
refetchOnWindowFocus: false,
retry: 2,
});
const refreshProfile = () => {
if (userId) {
console.log('[useProfile] Invalidating profile cache for userId:', userId);
queryClient.invalidateQueries({ queryKey: ['profile', userId] });
}
};
return {
...query,
refreshProfile,
};
}