diff --git a/api/discord/oauth/callback.ts b/api/discord/oauth/callback.ts index 9bca3a5f..66634eb0 100644 --- a/api/discord/oauth/callback.ts +++ b/api/discord/oauth/callback.ts @@ -369,10 +369,21 @@ export default async function handler(req: any, res: any) { ? "/onboarding" : "/dashboard"; - const redirectUrl = new URL( - nextPath, - process.env.VITE_API_BASE || "https://aethex.dev", - ); + // Determine the base URL from environment or request origin + let baseUrl = process.env.VITE_API_BASE || process.env.API_BASE; + + if (!baseUrl && req.headers.referer) { + // Extract base URL from referer header as fallback + const refererUrl = new URL(req.headers.referer); + baseUrl = refererUrl.origin; + } + + // Final fallback - should not be needed if env vars are set + if (!baseUrl) { + baseUrl = "https://aethex.dev"; + } + + const redirectUrl = new URL(nextPath, baseUrl); res.redirect(redirectUrl.toString()); } catch (error) { console.error("[Discord OAuth] Callback error:", error);