diff --git a/src/app/api/auth/oauth/[provider]/start/route.ts b/src/app/api/auth/oauth/[provider]/start/route.ts index 4af624e..4a9041a 100644 --- a/src/app/api/auth/oauth/[provider]/start/route.ts +++ b/src/app/api/auth/oauth/[provider]/start/route.ts @@ -1,4 +1,5 @@ import { appUrl } from "@/lib/base-path"; +import { externalRedirect } from "@/lib/http"; import { NextResponse } from "next/server"; import { authorizeUrl, @@ -27,7 +28,7 @@ export async function GET(req: Request, ctx: RouteContext<"/api/auth/oauth/[prov from: safeRedirectPath(url.searchParams.get("from")), remember: url.searchParams.get("remember") !== "0", }); - const res = NextResponse.redirect(authorizeUrl(provider, url.origin, nonce)); + const res = externalRedirect(authorizeUrl(provider, url.origin, nonce)); res.cookies.set(SOCIAL_STATE_COOKIE, cookie, { httpOnly: true, secure: process.env.NODE_ENV === "production", diff --git a/src/app/api/facebook/oauth/start/route.ts b/src/app/api/facebook/oauth/start/route.ts index cd55131..f32a8d6 100644 --- a/src/app/api/facebook/oauth/start/route.ts +++ b/src/app/api/facebook/oauth/start/route.ts @@ -3,6 +3,7 @@ import { NextResponse } from "next/server"; import { getCurrentUser, isSuperadmin } from "@/lib/auth/user"; import { authorizeUrl, createOAuthState, OAUTH_STATE_COOKIE } from "@/lib/facebook/oauth"; import { safeFacebookErrorMessage } from "@/lib/facebook/client"; +import { externalRedirect } from "@/lib/http"; /** SUPERADMIN only: redirects to the Facebook consent dialog. */ export async function GET(req: Request) { @@ -13,7 +14,7 @@ export async function GET(req: Request) { try { const { nonce, cookie } = await createOAuthState(user.id); - const res = NextResponse.redirect(authorizeUrl(origin, nonce)); + const res = externalRedirect(authorizeUrl(origin, nonce)); res.cookies.set(OAUTH_STATE_COOKIE, cookie, { httpOnly: true, secure: process.env.NODE_ENV === "production", diff --git a/src/lib/http.ts b/src/lib/http.ts index 16af744..85418f8 100644 --- a/src/lib/http.ts +++ b/src/lib/http.ts @@ -29,6 +29,22 @@ export function htmlRedirect(path: string): Response { return new Response(html, { headers: { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" } }); } +/** + * Sends the browser to an absolute external URL (Facebook / Google dialogs) with + * a 200 HTML meta-refresh instead of a 3xx `Location`. Behind an IIS ARR reverse + * proxy (loma.chat/pagiva) the machine-level `reverseRewriteHostInResponseHeaders` + * rewrites the host of any `Location` header to the public host, which turned + * https://www.facebook.com/v21.0/dialog/oauth into https://loma.chat/v21.0/... + * A response body is never rewritten. Returns a NextResponse so callers can set cookies. + */ +export function externalRedirect(url: string): NextResponse { + const target = new URL(url); + if (target.protocol !== "https:") throw new Error("externalRedirect requires an https URL"); + const safe = target.toString().replace(/&/g, "&").replace(/"/g, """).replace(//g, ">"); + const html = `กำลังไปยังหน้าเข้าสู่ระบบ…

กำลังไปยังหน้าเข้าสู่ระบบ… ไปต่อ

`; + return new NextResponse(html, { headers: { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" } }); +} + /** Cron routes authenticate with `Authorization: Bearer ${CRON_SECRET}` (Vercel Cron sends it). */ export function cronAuthorized(req: Request): boolean { const secret = process.env.CRON_SECRET;