From d487f5efe0347a27ea3d5a713b1e2fc52559577d Mon Sep 17 00:00:00 2001 From: Dmytro Stanchiev Date: Sat, 16 Aug 2025 20:34:57 -0400 Subject: [PATCH] a --- next.config.ts | 1 + src/app/api/auth/[...nextauth]/route.ts | 33 ++++++++++++++++++++++++- src/auth.ts | 3 +++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/next.config.ts b/next.config.ts index 4263181..91b42f1 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,6 +1,7 @@ import type { NextConfig } from "next"; const nextConfig: NextConfig = { + basePath: process.env.BASE_PATH, reactStrictMode: true, output: "standalone", images: { diff --git a/src/app/api/auth/[...nextauth]/route.ts b/src/app/api/auth/[...nextauth]/route.ts index 7c62e2d..cc1e3a0 100644 --- a/src/app/api/auth/[...nextauth]/route.ts +++ b/src/app/api/auth/[...nextauth]/route.ts @@ -1,2 +1,33 @@ +// import { handlers } from "@/auth"; +// export const { GET, POST } = handlers; +// +import { NextRequest } from "next/server"; + import { handlers } from "@/auth"; -export const { GET, POST } = handlers; + +const basePath = process.env.BASE_PATH ?? ""; + +function rewriteRequest(request: NextRequest) { + const { protocol, host, pathname } = request.nextUrl; + + const headers = request.headers; + // Host rewrite adopted from next-auth/packages/core/src/lib/utils/env.ts:createActionURL + const detectedHost = headers.get("x-forwarded-host") ?? host; + const detectedProtocol = headers.get("x-forwarded-proto") ?? protocol; + const _protocol = detectedProtocol.endsWith(":") + ? detectedProtocol + : detectedProtocol + ":"; + const url = new URL( + `${_protocol}//${detectedHost}${basePath}${pathname}${request.nextUrl.search}`, + ); + + return new NextRequest(url, request); +} + +export async function GET(request: NextRequest) { + return await handlers.GET(rewriteRequest(request)); +} + +export async function POST(request: NextRequest) { + return await handlers.POST(rewriteRequest(request)); +} diff --git a/src/auth.ts b/src/auth.ts index b8c940f..78ad6ef 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -32,7 +32,10 @@ export const providerMap = providers.map((provider) => { } }); +const basePath = process.env.BASE_PATH ?? '' + const config = { + basePath: `${basePath}/api/auth`, providers, pages: { signIn: "/signin",