Skip to content

Commit 5818fbf

Browse files
committed
feat(auth): auto-create default profile on magic link signin
1 parent cbef8c5 commit 5818fbf

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

app/auth/callback/route.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,34 @@ export async function GET(request: NextRequest) {
7171
)
7272
}
7373

74-
// If profile doesn't exist, it might be a new OAuth user where trigger failed or hasn't run yet,
75-
// or a manual signup where we need them to complete a profile.
76-
// However, for standard email signup with trigger, profile should exist.
7774
if (!existingProfile) {
78-
console.log('👤 New user/No profile detected, redirecting to signup...')
75+
console.log('👤 New user/No profile detected, creating default profile...')
7976

8077
const userData = {
8178
email: user.email,
82-
name: user.user_metadata.full_name || user.user_metadata.name || '',
83-
avatar_url: user.user_metadata.avatar_url || user.user_metadata.picture || '',
79+
name: user.user_metadata?.full_name || user.user_metadata?.name || user.email?.split('@')[0] || 'User',
80+
avatar: user.user_metadata?.avatar_url || user.user_metadata?.picture || '',
8481
}
8582

86-
// Redirect to signup to complete profile creation if needed
87-
const signupUrl = new URL('/signup', origin)
88-
signupUrl.searchParams.set('email', userData.email || '')
89-
if (userData.name) signupUrl.searchParams.set('name', userData.name)
90-
if (userData.avatar_url) signupUrl.searchParams.set('avatar', userData.avatar_url)
91-
92-
return NextResponse.redirect(signupUrl.toString())
83+
const { error: insertError } = await supabase
84+
.from('profiles')
85+
.insert({
86+
id: user.id,
87+
name: userData.name,
88+
language: 'en',
89+
avatar: userData.avatar
90+
})
91+
92+
if (insertError) {
93+
console.error('❌ Error creating profile:', insertError)
94+
return NextResponse.redirect(
95+
`${origin}/login?error=profile_creation_failed&message=${encodeURIComponent(insertError.message)}`
96+
)
97+
}
9398
}
9499

95-
// User exists, redirect to dashboard
96-
console.log('✅ Existing user found, redirecting to dashboard')
100+
// User profile exists or was just created, redirect to dashboard
101+
console.log('✅ User profile ready, redirecting to dashboard')
97102
return NextResponse.redirect(`${origin}/dashboard`)
98103

99104
} catch (error) {

0 commit comments

Comments
 (0)