Problem Statement:
I'm trying to create a protected API route in Next.js 15.2.4 using NextAuth.js 5.0.0-beta.25. However, when I try to build the project, I get the following error:
.next/types/app/api/test/route.ts:49:7
Type error: Type '{ __tag__: "GET"; __param_position__: "second"; __param_type__: AppRouteHandlerFnContext; }' does not satisfy the constraint 'ParamCheck<RouteContext>'.
The types of '__param_type__.params' are incompatible between these types.
Type 'Record<string, string | string[]> | undefined' is not assignable to type 'Promise<any>'.
Type 'undefined' is not assignable to type 'Promise<any>'.
47 | Diff<
48 | ParamCheck<RouteContext>,
> 49 | {
| ^
50 | __tag__: 'GET'
51 | __param_position__: 'second'
52 | __param_type__: SecondArg<MaybeField<TEntry, 'GET'>>
Next.js build worker exited with code: 1 and signal: null
error: script "build" exited with code 1.
Code That Causes the Error:
import { auth } from "@/auth";
import { NextResponse } from "next/server";
export const GET = auth(async function GET(req) {
if (req.auth) return NextResponse.json(req.auth);
return NextResponse.json({ message: "Not authenticated" }, { status: 401 });
});
- Explicitly defining ctx (context) as a second parameter:
export const GET = auth(async function GET(req, ctx) {
if (req.auth) return NextResponse.json(req.auth);
return NextResponse.json({ message: "Not authenticated" }, { status: 401 });
});
→ Still produces the same error.
- Explicitly typing ctx as { params?: Record<string, string | string[]> }:
export const GET = auth(async function GET(req, ctx: { params?: Record<string, string | string[]> }) {
if (req.auth) return NextResponse.json(req.auth);
return NextResponse.json({ message: "Not authenticated" }, { status: 401 });
});
→ Still no luck.
- Checking for AppRouteHandler type in Next.js:
I tried importing AppRouteHandler from "next/dist/server/web/types", but it does not exist.
Expected Behavior:
The API route should work properly without type errors, allowing authentication via NextAuth.js in Next.js 15.
My Environment:
- Next.js Version: 15.2.4
- NextAuth.js Version: 5.0.0-beta.25