fix: handle missing profile on login with auto-creation
All checks were successful
Build & Deploy / build-and-deploy (push) Successful in 27s

This commit is contained in:
ordinarthur 2026-04-12 13:38:21 +02:00
parent 3d06a27a73
commit f80131804e

View File

@ -26,11 +26,27 @@ export const useAuthStore = create<AuthState>((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)
.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 })
},