fix: handle missing profile on login with auto-creation
All checks were successful
Build & Deploy / build-and-deploy (push) Successful in 27s
All checks were successful
Build & Deploy / build-and-deploy (push) Successful in 27s
This commit is contained in:
parent
3d06a27a73
commit
f80131804e
@ -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)
|
||||
.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 })
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user