Files
PAGIVA/src/app/api/auth/oauth/[provider]/start/route.ts
T
Loma Bot Ops dc6ee09a55 Use HTML meta-refresh for external OAuth redirects behind IIS ARR
ARR's machine-level reverseRewriteHostInResponseHeaders rewrote the host of
Location headers, sending Facebook/Google login dialogs to loma.chat/v21.0/...
Serve a 200 HTML hop to the provider URL instead of a 3xx Location.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 16:44:26 +07:00

42 lines
1.4 KiB
TypeScript

import { appUrl } from "@/lib/base-path";
import { externalRedirect } from "@/lib/http";
import { NextResponse } from "next/server";
import {
authorizeUrl,
createSocialState,
enabledProviders,
parseProvider,
safeRedirectPath,
SOCIAL_STATE_COOKIE,
SOCIAL_STATE_PATH,
SOCIAL_STATE_TTL_SECONDS,
} from "@/lib/auth/social";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
/** Public: sends the browser to the Google / Facebook sign-in dialog. */
export async function GET(req: Request, ctx: RouteContext<"/api/auth/oauth/[provider]/start">) {
const { provider: raw } = await ctx.params;
const url = new URL(req.url);
const provider = parseProvider(raw);
if (!provider || !enabledProviders().includes(provider)) {
return NextResponse.redirect(`${appUrl(url.origin)}/login?error=not_configured`);
}
const { nonce, cookie } = await createSocialState(provider, {
from: safeRedirectPath(url.searchParams.get("from")),
remember: url.searchParams.get("remember") !== "0",
});
const res = externalRedirect(authorizeUrl(provider, url.origin, nonce));
res.cookies.set(SOCIAL_STATE_COOKIE, cookie, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
// Lax: must survive the top-level redirect back from the provider.
sameSite: "lax",
path: SOCIAL_STATE_PATH,
maxAge: SOCIAL_STATE_TTL_SECONDS,
});
return res;
}