**Problem:** Password reset emails redirected users to homepage with ?code= parameter, leaving users stuck without ability to set new password. **Root Cause:** Auth callback route didn't detect password recovery flow. When Supabase sent users back with type=recovery parameter, the callback defaulted to /epd/clients redirect instead of /update-password. **Solution:** - Detect `type=recovery` parameter in auth callback route - Always redirect password recovery flows to /update-password - Preserves existing flows (signup, magic link) unchanged **Flow now:** 1. User clicks reset link in email 2. Supabase verifies token → redirect to homepage with ?code=&type=recovery 3. Middleware redirects to /auth/callback?code=&type=recovery 4. Callback detects type=recovery → redirect to /update-password ✅ 5. User can set new password This fix ensures password reset works regardless of Supabase redirect_to configuration, making the flow more resilient. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
/**
|
|
* Auth Callback Route
|
|
*
|
|
* Handles Supabase auth callbacks after magic link click
|
|
* or OAuth provider authentication
|
|
*/
|
|
|
|
import { createClient } from '@/lib/auth/server'
|
|
import { NextResponse } from 'next/server'
|
|
import type { NextRequest } from 'next/server'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const requestUrl = new URL(request.url)
|
|
const code = requestUrl.searchParams.get('code')
|
|
const type = requestUrl.searchParams.get('type') // recovery, signup, etc
|
|
const next = requestUrl.searchParams.get('next') ?? '/epd/clients'
|
|
|
|
if (code) {
|
|
const supabase = await createClient()
|
|
|
|
// Exchange code for session
|
|
const { data, error } = await supabase.auth.exchangeCodeForSession(code)
|
|
|
|
if (!error && data.user) {
|
|
// Password recovery flow - always go to update-password
|
|
if (type === 'recovery') {
|
|
return NextResponse.redirect(new URL('/update-password', request.url))
|
|
}
|
|
|
|
// Check if new user (first login)
|
|
const isNewUser = new Date(data.user.created_at).getTime() ===
|
|
new Date(data.user.last_sign_in_at || '').getTime()
|
|
|
|
// Check if user signed up with password (has identity provider = 'email')
|
|
const hasPassword = data.user.identities?.some(
|
|
identity => identity.provider === 'email'
|
|
)
|
|
|
|
if (isNewUser && !hasPassword) {
|
|
// Redirect new magic link users to set-password page (optional)
|
|
return NextResponse.redirect(new URL('/set-password', request.url))
|
|
}
|
|
|
|
// Redirect to the specified next URL or default to /epd/clients
|
|
// This includes: password signups, confirmed email users, and returning users
|
|
return NextResponse.redirect(new URL(next, request.url))
|
|
}
|
|
}
|
|
|
|
// Return the user to an error page with instructions
|
|
return NextResponse.redirect(
|
|
new URL('/login?error=auth_callback_error', request.url)
|
|
)
|
|
}
|