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..93ed577 100644 --- a/src/app/api/auth/[...nextauth]/route.ts +++ b/src/app/api/auth/[...nextauth]/route.ts @@ -1,2 +1,31 @@ -import { handlers } from "@/auth"; -export const { GET, POST } = handlers; +// import { handlers } from "@/auth"; +// export const { GET, POST } = handlers; +// +import { NextRequest } from 'next/server' + +import { handlers } from '@/auth' + +const basePath = process.env.BASE_PATH ?? '' + +function rewriteRequest(request) { + let { 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, props) { + return await handlers.GET(rewriteRequest(request)) +} + +export async function POST(request, props) { + 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",