Skip to content

Commit 153b9c3

Browse files
committed
Refactor user profile check in auth callback route
Updated the authentication callback to check for existing user profiles in the 'profiles' table instead of the 'users' table. Improved error handling and streamlined the logic for redirecting new users to the signup page if their profile does not exist.
1 parent 9fae5f7 commit 153b9c3

1 file changed

Lines changed: 21 additions & 36 deletions

File tree

app/auth/callback/route.ts

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function GET(request: NextRequest) {
1717

1818
try {
1919
const cookieStore = await cookies()
20-
20+
2121
// Create server-side Supabase client
2222
const supabase = createServerClient(
2323
process.env.NEXT_PUBLIC_SUPABASE_URL!,
@@ -38,10 +38,10 @@ export async function GET(request: NextRequest) {
3838
)
3939

4040
console.log('🔄 Exchanging code for session...')
41-
41+
4242
// Exchange code for session
4343
const { data: { session }, error: sessionError } = await supabase.auth.exchangeCodeForSession(code)
44-
44+
4545
if (sessionError) {
4646
console.error('❌ Error exchanging code for session:', sessionError)
4747
return NextResponse.redirect(`${origin}/login?error=auth_failed&message=${encodeURIComponent(sessionError.message)}`)
@@ -55,54 +55,39 @@ export async function GET(request: NextRequest) {
5555
const user = session.user
5656
console.log('✅ Session created for user:', user.email)
5757

58-
// Check if this is a new user (first time signing in with Google)
59-
console.log('🔍 Checking if user exists in database...')
60-
const { data: existingUser, error: userError } = await supabase
61-
.from('users')
58+
// Check if user profile exists (created by trigger on signup)
59+
console.log('🔍 Checking if user profile exists...')
60+
const { data: existingProfile, error: profileError } = await supabase
61+
.from('profiles')
6262
.select('id, email')
63-
.eq('email', user.email)
63+
.eq('id', user.id)
6464
.single()
6565

66-
// Handle potential errors from the user lookup
67-
if (userError) {
68-
// Supabase returns code 'PGRST116' when .single() finds no rows (i.e., user not found)
69-
if (userError.code === 'PGRST116') {
70-
console.log('ℹ️ User not found in database (expected for new user):', userError.message)
71-
} else {
72-
console.error('❌ Error checking if user exists in database:', userError)
73-
return NextResponse.redirect(
74-
`${origin}/login?error=user_lookup_failed&message=${encodeURIComponent(userError.message)}`
75-
)
76-
}
77-
}
78-
if (userError) {
79-
console.error('❌ Error checking if user exists:', userError)
66+
// Handle potential errors from the profile lookup
67+
if (profileError && profileError.code !== 'PGRST116') {
68+
console.error('❌ Error checking if profile exists:', profileError)
8069
return NextResponse.redirect(
81-
`${origin}/login?error=user_lookup_failed&message=${encodeURIComponent(
82-
userError.message,
83-
)}`,
70+
`${origin}/login?error=profile_lookup_failed&message=${encodeURIComponent(profileError.message)}`
8471
)
8572
}
8673

87-
// If user doesn't exist in our users table, redirect to signup to complete profile
88-
if (!existingUser) {
89-
console.log('👤 New user detected, redirecting to signup...')
90-
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.
77+
if (!existingProfile) {
78+
console.log('👤 New user/No profile detected, redirecting to signup...')
79+
9180
const userData = {
9281
email: user.email,
9382
name: user.user_metadata.full_name || user.user_metadata.name || '',
9483
avatar_url: user.user_metadata.avatar_url || user.user_metadata.picture || '',
95-
provider: 'google'
9684
}
9785

98-
// Redirect to signup with user data as query params
86+
// Redirect to signup to complete profile creation if needed
9987
const signupUrl = new URL('/signup', origin)
100-
signupUrl.searchParams.set('google', 'true')
10188
signupUrl.searchParams.set('email', userData.email || '')
102-
signupUrl.searchParams.set('name', userData.name)
103-
if (userData.avatar_url) {
104-
signupUrl.searchParams.set('avatar', userData.avatar_url)
105-
}
89+
if (userData.name) signupUrl.searchParams.set('name', userData.name)
90+
if (userData.avatar_url) signupUrl.searchParams.set('avatar', userData.avatar_url)
10691

10792
return NextResponse.redirect(signupUrl.toString())
10893
}

0 commit comments

Comments
 (0)