最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Next.js 15 API Route Type Error with NextAuth 5 - Stack Overflow

programmeradmin1浏览0评论

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 });
});
  1. 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.

  1. 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.

  1. 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
发布评论

评论列表(0)

  1. 暂无评论