diff --git a/src/stores/auth.ts b/src/stores/auth.ts index 1db84e9..a4be5ca 100644 --- a/src/stores/auth.ts +++ b/src/stores/auth.ts @@ -26,11 +26,27 @@ export const useAuthStore = create((set, get) => ({ const { user } = get() if (!user) return set({ profile: null }) - const { data } = await supabase + const { data, error } = await supabase .from('profiles') .select('*') .eq('id', user.id) - .single() + .maybeSingle() + + if (error) { + console.error('Error fetching profile:', error) + return set({ profile: null }) + } + + // Auto-create profile if it doesn't exist (trigger may have failed) + if (!data) { + const username = user.user_metadata?.username || user.email?.split('@')[0] || 'user' + const { data: newProfile } = await supabase + .from('profiles') + .insert({ id: user.id, username }) + .select() + .single() + return set({ profile: newProfile }) + } set({ profile: data }) },