From f80131804e94aee0cce3f06dd7d12522988f2c09 Mon Sep 17 00:00:00 2001 From: ordinarthur <@arthurbarre.js@gmail.com> Date: Sun, 12 Apr 2026 13:38:21 +0200 Subject: [PATCH] fix: handle missing profile on login with auto-creation --- src/stores/auth.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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 }) },