Merge pull request #1391 from ffrankan/fix/auth-callback-self-hosted-redirect

Fix auth callback redirect issues in self-hosted deployments
This commit is contained in:
Marko Kraemer 2025-08-20 17:07:38 -07:00 committed by GitHub
commit 5e1e82f67a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 7 deletions

View File

@ -3,15 +3,18 @@ import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
const { searchParams, origin } = new URL(request.url)
const { searchParams } = new URL(request.url)
const code = searchParams.get('code')
const next = searchParams.get('next') ?? '/dashboard'
const next = searchParams.get('returnUrl') ?? '/dashboard'
// Use configured URL instead of parsed origin to avoid 0.0.0.0 issues in self-hosted environments
const baseUrl = process.env.NEXT_PUBLIC_URL || 'http://localhost:3000'
const error = searchParams.get('error')
const errorDescription = searchParams.get('error_description')
if (error) {
console.error('❌ Auth callback error:', error, errorDescription)
return NextResponse.redirect(`${origin}/auth?error=${encodeURIComponent(error)}`)
return NextResponse.redirect(`${baseUrl}/auth?error=${encodeURIComponent(error)}`)
}
if (code) {
@ -22,15 +25,15 @@ export async function GET(request: NextRequest) {
if (error) {
console.error('❌ Error exchanging code for session:', error)
return NextResponse.redirect(`${origin}/auth?error=${encodeURIComponent(error.message)}`)
return NextResponse.redirect(`${baseUrl}/auth?error=${encodeURIComponent(error.message)}`)
}
// URL to redirect to after sign in process completes
return NextResponse.redirect(`${origin}${next}`)
return NextResponse.redirect(`${baseUrl}${next}`)
} catch (error) {
console.error('❌ Unexpected error in auth callback:', error)
return NextResponse.redirect(`${origin}/auth?error=unexpected_error`)
return NextResponse.redirect(`${baseUrl}/auth?error=unexpected_error`)
}
}
return NextResponse.redirect(`${origin}/auth`)
return NextResponse.redirect(`${baseUrl}/auth`)
}