From c647a6c5c09c90815a6194a556cd0878ae4f5852 Mon Sep 17 00:00:00 2001 From: incognitotgt Date: Wed, 12 Jun 2024 08:33:06 -0400 Subject: [PATCH] FEAT: multidomain support https://github.com/nextauthjs/next-auth/issues/10928#issuecomment-2144241314 --- src/app/api/auth/[...nextauth]/route.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/app/api/auth/[...nextauth]/route.ts b/src/app/api/auth/[...nextauth]/route.ts index 73228a0..4bbfff4 100644 --- a/src/app/api/auth/[...nextauth]/route.ts +++ b/src/app/api/auth/[...nextauth]/route.ts @@ -1,2 +1,22 @@ import { handlers } from "@/lib/auth"; -export const { GET, POST } = handlers; +import { NextRequest } from "next/server"; + +const reqWithTrustedOrigin = (req: NextRequest): NextRequest => { + const proto = req.headers.get("x-forwarded-proto"); + const host = req.headers.get("x-forwarded-host"); + if (!proto || !host) { + console.warn("Missing x-forwarded-proto or x-forwarded-host headers."); + return req; + } + const envOrigin = `${proto}://${host}`; + const { href, origin } = req.nextUrl; + return new NextRequest(href.replace(origin, envOrigin), req); +}; + +export const GET = (req: NextRequest) => { + return handlers.GET(reqWithTrustedOrigin(req)); +}; + +export const POST = (req: NextRequest) => { + return handlers.POST(reqWithTrustedOrigin(req)); +};