Fixed redirecting to localhost
This commit is contained in:
@@ -12,12 +12,14 @@ export async function GET(request: NextRequest) {
|
|||||||
const guildId = searchParams.get("guild_id");
|
const guildId = searchParams.get("guild_id");
|
||||||
const error = searchParams.get("error");
|
const error = searchParams.get("error");
|
||||||
|
|
||||||
|
const baseUrl = process.env.APP_URL || request.url;
|
||||||
|
|
||||||
if (error) {
|
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) {
|
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();
|
const cookieStore = await cookies();
|
||||||
@@ -26,12 +28,12 @@ export async function GET(request: NextRequest) {
|
|||||||
|
|
||||||
// 1. Verify State
|
// 1. Verify State
|
||||||
if (!storedState || state !== storedState) {
|
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)
|
// 2. Verify Guild ID Match (Optional but recommended extra layer)
|
||||||
if (storedGuildId && guildId !== storedGuildId) {
|
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
|
// 3. CRITICAL: Check Beta Server Eligibility
|
||||||
@@ -40,14 +42,14 @@ export async function GET(request: NextRequest) {
|
|||||||
|
|
||||||
if (!isBeta) {
|
if (!isBeta) {
|
||||||
console.warn(`Blocked attempt to add bot to non-beta server: ${guildId}`);
|
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)
|
// 4. Exchange Code for Token (Finalize Bot Join)
|
||||||
const appUrl = process.env.APP_URL;
|
const appUrl = process.env.APP_URL;
|
||||||
if (!appUrl) {
|
if (!appUrl) {
|
||||||
console.error("APP_URL env var is not set");
|
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`;
|
const redirectUri = `${appUrl}/api/oauth/callback`;
|
||||||
|
|
||||||
@@ -68,7 +70,7 @@ export async function GET(request: NextRequest) {
|
|||||||
if (!tokenResponse.ok) {
|
if (!tokenResponse.ok) {
|
||||||
const errorText = await tokenResponse.text();
|
const errorText = await tokenResponse.text();
|
||||||
console.error("Failed to exchange token:", errorText);
|
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
|
// Clean up cookies
|
||||||
@@ -76,10 +78,10 @@ export async function GET(request: NextRequest) {
|
|||||||
cookieStore.delete("oauth_invite_guild");
|
cookieStore.delete("oauth_invite_guild");
|
||||||
|
|
||||||
// Success!
|
// 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) {
|
} catch (error) {
|
||||||
console.error("Callback handler error:", 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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user