Merge branch 'main' into db-fixes-and-patches
This commit is contained in:
@@ -628,6 +628,60 @@ export const userManagement = {
|
||||
|
||||
if (error) throw error
|
||||
return data
|
||||
},
|
||||
|
||||
// Sync OAuth user - ensures user profile exists for OAuth-authenticated users
|
||||
async syncOAuthUser(): Promise<void> {
|
||||
try {
|
||||
const { data: { user: authUser }, error: authError } = await supabase.auth.getUser()
|
||||
|
||||
if (authError || !authUser) {
|
||||
console.warn('No authenticated user found for OAuth sync')
|
||||
return
|
||||
}
|
||||
|
||||
// Check if user profile already exists
|
||||
const { data: existingProfile, error: checkError } = await supabase
|
||||
.from('user_profiles')
|
||||
.select('id')
|
||||
.eq('id', authUser.id)
|
||||
.single()
|
||||
|
||||
// If profile already exists, no need to create it
|
||||
if (existingProfile && !checkError) {
|
||||
console.log('User profile already exists for user:', authUser.id)
|
||||
return
|
||||
}
|
||||
|
||||
// If error is not "no rows returned", it's a real error
|
||||
if (checkError && checkError.code !== 'PGRST116') {
|
||||
console.error('Error checking for existing profile:', checkError)
|
||||
return
|
||||
}
|
||||
|
||||
// Create user profile for new OAuth user
|
||||
const { error: insertError } = await supabase
|
||||
.from('user_profiles')
|
||||
.insert({
|
||||
id: authUser.id,
|
||||
email: authUser.email || '',
|
||||
status: 'active'
|
||||
})
|
||||
|
||||
if (insertError) {
|
||||
// Ignore "duplicate key value" errors in case of race condition
|
||||
if (insertError.code === '23505') {
|
||||
console.log('User profile was already created (race condition handled)')
|
||||
return
|
||||
}
|
||||
console.error('Error creating user profile for OAuth user:', insertError)
|
||||
return
|
||||
}
|
||||
|
||||
console.log('Successfully created user profile for OAuth user:', authUser.id)
|
||||
} catch (error) {
|
||||
console.error('Unexpected error in syncOAuthUser:', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user