I answered a similar question here:
In summary, this is currently still an issue in next-auth v5 beta. You can read up on them at:
The two workarounds from the first link are:
git clone -b fix-base-path https://github.com/k3k8/next-auth.git cd next-auth corepack enable pnpm pnpm install pnpm build cd packages/next-auth/ pnpm pack
This will generate a package file named something like
next-auth-5.0.0-beta.20.tgz
.Move this file to your project directory and install it by updating your
package.json
:{ "dependencies": { "next-auth": "./next-auth-5.0.0-beta.20.tgz" } }
Then run
pnpm install
@ThangHuuVu's solution (source):
// app/api/auth/[...nextauth]/route.ts import { handlers } from "auth"; import { NextRequest } from "next/server"; const reqWithBasePath = (req: NextRequest, basePath = "/authjs") => { const baseUrl = new URL( (req.headers.get("x-forwarded-proto") ?? "https") + "://" + (req.headers.get("x-forwarded-host") ?? req.nextUrl.host) ); const urlWithBasePath = new URL( `${basePath}${req.nextUrl.pathname}${req.nextUrl.search}`, baseUrl ); return new NextRequest(urlWithBasePath.toString(), req); }; export const GET = (req: NextRequest) => { const nextReq = reqWithBasePath(req); return handlers.GET(nextReq); }; export const POST = (req: NextRequest) => { const nextReq = reqWithBasePath(req); return handlers.POST(nextReq); };