callback / debug
This commit is contained in:
@@ -29,63 +29,95 @@ export async function GET(request: NextRequest) {
|
||||
allParams: Object.fromEntries(requestUrl.searchParams)
|
||||
})
|
||||
|
||||
// Use code, token, or token_hash (different Supabase versions use different params)
|
||||
const authCode = code || token || token_hash
|
||||
const supabase = await createClient()
|
||||
let data: any = null
|
||||
let error: any = null
|
||||
|
||||
if (authCode) {
|
||||
const supabase = await createClient()
|
||||
// Check if this is a token_hash flow (email magic links, password reset)
|
||||
if (token_hash && type) {
|
||||
console.log('🔐 Using verifyOtp for token_hash flow')
|
||||
|
||||
// Exchange code for session
|
||||
const { data, error } = await supabase.auth.exchangeCodeForSession(authCode)
|
||||
// For email links (magic link, password reset), use verifyOtp
|
||||
const result = await supabase.auth.verifyOtp({
|
||||
token_hash,
|
||||
type: type as any // 'recovery', 'signup', 'magiclink', etc.
|
||||
})
|
||||
|
||||
if (error) {
|
||||
console.error('❌ Auth Callback Exchange Error:', {
|
||||
message: error.message,
|
||||
status: error.status,
|
||||
code: error.code
|
||||
})
|
||||
}
|
||||
data = result.data
|
||||
error = result.error
|
||||
}
|
||||
// Check if this is a PKCE code flow (OAuth callbacks)
|
||||
else if (code) {
|
||||
console.log('🔐 Using exchangeCodeForSession for PKCE flow')
|
||||
|
||||
if (!error && data.user) {
|
||||
// PRIORITY: If next is /update-password, ALWAYS go there (password reset flow)
|
||||
if (next === '/update-password' || next.includes('update-password')) {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.log('✅ Redirecting to /update-password (next parameter detected)')
|
||||
}
|
||||
return NextResponse.redirect(new URL('/update-password', request.url))
|
||||
}
|
||||
const result = await supabase.auth.exchangeCodeForSession(code)
|
||||
data = result.data
|
||||
error = result.error
|
||||
}
|
||||
// Fallback for older 'token' parameter
|
||||
else if (token) {
|
||||
console.log('🔐 Using exchangeCodeForSession for legacy token flow')
|
||||
|
||||
// Password recovery flow detection:
|
||||
// Check type parameter (Supabase adds this automatically for recovery)
|
||||
if (type === 'recovery') {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.log('✅ Redirecting to /update-password (recovery type detected)')
|
||||
}
|
||||
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))
|
||||
}
|
||||
const result = await supabase.auth.exchangeCodeForSession(token)
|
||||
data = result.data
|
||||
error = result.error
|
||||
}
|
||||
|
||||
// Return the user to an error page with instructions
|
||||
// Handle errors
|
||||
if (error) {
|
||||
console.error('❌ Auth Callback Error:', {
|
||||
message: error.message,
|
||||
status: error.status,
|
||||
code: error.code
|
||||
})
|
||||
// Redirect to debug page with error info
|
||||
const debugParams = new URLSearchParams(requestUrl.searchParams)
|
||||
debugParams.set('auth_error', error.message)
|
||||
debugParams.set('error_code', error.code || 'unknown')
|
||||
return NextResponse.redirect(
|
||||
new URL('/auth/debug?' + debugParams.toString(), request.url)
|
||||
)
|
||||
}
|
||||
|
||||
// Success - user authenticated
|
||||
if (data?.user) {
|
||||
console.log('✅ Auth successful, user:', data.user.email)
|
||||
|
||||
// PRIORITY: If next is /update-password, ALWAYS go there (password reset flow)
|
||||
if (next === '/update-password' || next.includes('update-password')) {
|
||||
console.log('✅ Redirecting to /update-password (next parameter detected)')
|
||||
return NextResponse.redirect(new URL('/update-password', request.url))
|
||||
}
|
||||
|
||||
// Password recovery flow detection:
|
||||
// Check type parameter (Supabase adds this automatically for recovery)
|
||||
if (type === 'recovery') {
|
||||
console.log('✅ Redirecting to /update-password (recovery type detected)')
|
||||
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 debug page to see what parameters came through
|
||||
console.error('❌ Auth Callback Failed - No auth token received')
|
||||
return NextResponse.redirect(
|
||||
new URL('/login?error=auth_callback_error', request.url)
|
||||
new URL('/auth/debug?' + requestUrl.searchParams.toString(), request.url)
|
||||
)
|
||||
}
|
||||
|
||||
119
app/auth/debug/page.tsx
Normal file
119
app/auth/debug/page.tsx
Normal file
@@ -0,0 +1,119 @@
|
||||
'use client'
|
||||
|
||||
import { useSearchParams } from 'next/navigation'
|
||||
import { Suspense } from 'react'
|
||||
|
||||
function DebugContent() {
|
||||
const searchParams = useSearchParams()
|
||||
|
||||
// Get all parameters
|
||||
const allParams: Record<string, string> = {}
|
||||
searchParams.forEach((value, key) => {
|
||||
allParams[key] = value
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-slate-50 p-8">
|
||||
<div className="max-w-4xl mx-auto">
|
||||
<div className="bg-white rounded-lg shadow-lg p-8">
|
||||
<h1 className="text-3xl font-bold text-slate-900 mb-6">
|
||||
🔍 Auth Debug Page
|
||||
</h1>
|
||||
|
||||
<div className="mb-8">
|
||||
<h2 className="text-xl font-semibold text-slate-700 mb-4">
|
||||
Full URL
|
||||
</h2>
|
||||
<div className="bg-slate-100 p-4 rounded-lg font-mono text-sm break-all">
|
||||
{typeof window !== 'undefined' ? window.location.href : 'Loading...'}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mb-8">
|
||||
<h2 className="text-xl font-semibold text-slate-700 mb-4">
|
||||
URL Parameters ({Object.keys(allParams).length})
|
||||
</h2>
|
||||
{Object.keys(allParams).length > 0 ? (
|
||||
<div className="space-y-3">
|
||||
{Object.entries(allParams).map(([key, value]) => (
|
||||
<div key={key} className="bg-slate-100 p-4 rounded-lg">
|
||||
<div className="font-mono text-sm">
|
||||
<span className="text-teal-600 font-semibold">{key}</span>
|
||||
<span className="text-slate-500"> = </span>
|
||||
<span className="text-slate-900">{value}</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="bg-amber-50 border border-amber-200 rounded-lg p-4 text-amber-800">
|
||||
⚠️ No URL parameters found
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="border-t pt-6">
|
||||
<h2 className="text-xl font-semibold text-slate-700 mb-4">
|
||||
Expected Parameters for Password Reset
|
||||
</h2>
|
||||
<div className="space-y-2 text-sm">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={`w-3 h-3 rounded-full ${allParams.token_hash ? 'bg-green-500' : 'bg-red-500'}`} />
|
||||
<code className="text-slate-700">token_hash</code>
|
||||
<span className="text-slate-500">
|
||||
{allParams.token_hash ? '✅ Present' : '❌ Missing'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={`w-3 h-3 rounded-full ${allParams.type === 'recovery' ? 'bg-green-500' : 'bg-amber-500'}`} />
|
||||
<code className="text-slate-700">type=recovery</code>
|
||||
<span className="text-slate-500">
|
||||
{allParams.type === 'recovery' ? '✅ Correct' : allParams.type ? `⚠️ Wrong: ${allParams.type}` : '❌ Missing'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={`w-3 h-3 rounded-full ${allParams.next === '/update-password' ? 'bg-green-500' : 'bg-amber-500'}`} />
|
||||
<code className="text-slate-700">next=/update-password</code>
|
||||
<span className="text-slate-500">
|
||||
{allParams.next === '/update-password' ? '✅ Correct' : allParams.next ? `⚠️ Wrong: ${allParams.next}` : '❌ Missing'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-8 pt-6 border-t">
|
||||
<h2 className="text-xl font-semibold text-slate-700 mb-4">
|
||||
Quick Actions
|
||||
</h2>
|
||||
<div className="flex gap-4">
|
||||
<a
|
||||
href="/login"
|
||||
className="px-4 py-2 bg-teal-600 text-white rounded-lg hover:bg-teal-700 transition-colors"
|
||||
>
|
||||
Go to Login
|
||||
</a>
|
||||
<a
|
||||
href="/reset-password"
|
||||
className="px-4 py-2 bg-slate-600 text-white rounded-lg hover:bg-slate-700 transition-colors"
|
||||
>
|
||||
Reset Password
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function DebugPage() {
|
||||
return (
|
||||
<Suspense fallback={
|
||||
<div className="min-h-screen bg-slate-50 flex items-center justify-center">
|
||||
<div className="text-slate-600">Loading debug info...</div>
|
||||
</div>
|
||||
}>
|
||||
<DebugContent />
|
||||
</Suspense>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user