diff --git a/web/app/api/oauth/callback/route.ts b/web/app/api/oauth/callback/route.ts index 7d72518..96eb71b 100644 --- a/web/app/api/oauth/callback/route.ts +++ b/web/app/api/oauth/callback/route.ts @@ -12,12 +12,14 @@ export async function GET(request: NextRequest) { const guildId = searchParams.get("guild_id"); const error = searchParams.get("error"); + const baseUrl = process.env.APP_URL || request.url; + if (error) { - return NextResponse.redirect(new URL("/dashboard?error=access_denied", request.url)); + return NextResponse.redirect(new URL("/dashboard?error=access_denied", baseUrl)); } if (!code || !state || !guildId) { - return NextResponse.redirect(new URL("/dashboard?error=invalid_request", request.url)); + return NextResponse.redirect(new URL("/dashboard?error=invalid_request", baseUrl)); } const cookieStore = await cookies(); @@ -26,12 +28,12 @@ export async function GET(request: NextRequest) { // 1. Verify State if (!storedState || state !== storedState) { - return NextResponse.redirect(new URL("/dashboard?error=state_mismatch", request.url)); + return NextResponse.redirect(new URL("/dashboard?error=state_mismatch", baseUrl)); } // 2. Verify Guild ID Match (Optional but recommended extra layer) if (storedGuildId && guildId !== storedGuildId) { - return NextResponse.redirect(new URL("/dashboard?error=guild_mismatch", request.url)); + return NextResponse.redirect(new URL("/dashboard?error=guild_mismatch", baseUrl)); } // 3. CRITICAL: Check Beta Server Eligibility @@ -40,14 +42,14 @@ export async function GET(request: NextRequest) { if (!isBeta) { console.warn(`Blocked attempt to add bot to non-beta server: ${guildId}`); - return NextResponse.redirect(new URL("/dashboard?error=not_beta_server", request.url)); + return NextResponse.redirect(new URL("/dashboard?error=not_beta_server", baseUrl)); } // 4. Exchange Code for Token (Finalize Bot Join) const appUrl = process.env.APP_URL; if (!appUrl) { console.error("APP_URL env var is not set"); - return NextResponse.redirect(new URL("/dashboard?error=config_error", request.url)); + return NextResponse.redirect(new URL("/dashboard?error=config_error", baseUrl)); } const redirectUri = `${appUrl}/api/oauth/callback`; @@ -68,7 +70,7 @@ export async function GET(request: NextRequest) { if (!tokenResponse.ok) { const errorText = await tokenResponse.text(); console.error("Failed to exchange token:", errorText); - return NextResponse.redirect(new URL("/dashboard?error=token_exchange_failed", request.url)); + return NextResponse.redirect(new URL("/dashboard?error=token_exchange_failed", baseUrl)); } // Clean up cookies @@ -76,10 +78,10 @@ export async function GET(request: NextRequest) { cookieStore.delete("oauth_invite_guild"); // Success! - return NextResponse.redirect(new URL(`/dashboard?success=bot_added&guild_id=${guildId}`, request.url)); + return NextResponse.redirect(new URL(`/dashboard?success=bot_added&guild_id=${guildId}`, baseUrl)); } catch (error) { console.error("Callback handler error:", error); - return NextResponse.redirect(new URL("/dashboard?error=internal_server_error", request.url)); + return NextResponse.redirect(new URL("/dashboard?error=internal_server_error", process.env.APP_URL || request.url)); } }