Next.js项目中NextAuth.js AuthOptions类型不兼容错误修复
修复NextAuth.js中AuthOptions类型不兼容的TypeScript错误
问题场景
开发Next.js项目时使用NextAuth.js,定义authOptions时出现TypeScript错误,提示对象与预期的AuthOptions类型不兼容。
原代码
import bcrypt from "bcrypt"; import NextAuth, { AuthOptions } from "next-auth"; import CredentialsProvider from "next-auth/providers/credentials"; import { PrismaAdapter } from "@next-auth/prisma-adapter"; import prisma from "@/app/libs/prismadb"; export const authOptions: AuthOptions = { adapter: PrismaAdapter(prisma), providers: [ CredentialsProvider({ name: "credentials", credentials: { email: { label: "email", type: "text" }, password: { label: "password", type: "password" }, }, async authorize(credentials) { if (!credentials?.email || !credentials?.password) { throw new Error("Invalid credentials 1"); } const user = await prisma.user.findUnique({ where: { email: credentials.email, }, }); const isCorrectPassword = user ? await bcrypt.compare( credentials.password, user.hashedPassword || "" ) : false; if (!isCorrectPassword) { throw new Error("Invalid credentials 2"); } return user; }, }), ], debug: process.env.NODE_ENV === "development", session: { strategy: "jwt", }, secret: process.env.NEXTAUTH_SECRET, }; const handler = NextAuth(authOptions); export { handler as GET, handler as POST };
错误信息
"Type error: Type 'OmitWithTag<typeof import("D:/Projects/NextJs/Adminpanel/app/api/auth/[...nextauth]/route"), "config" | "generateStaticParams" | ...>' does not satisfy the constraint '{ [x: string]: never; }'. Property 'authOptions' is incompatible with index signature. Type 'AuthOptions' is not assignable to type 'never'."
解决方案
错误的核心原因是:Next.js App Router的route.ts路由文件只能导出HTTP方法处理函数(如GET、POST),不允许导出其他变量。authOptions的导出违反了这个约束,导致TypeScript报错,和AuthOptions类型本身无关。
方法1:抽离配置到单独文件(推荐)
将authOptions移到单独的配置文件中,便于后续复用,然后在路由文件中导入使用。
- 创建
@/app/libs/auth.ts文件,写入配置:
import bcrypt from "bcrypt"; import { AuthOptions } from "next-auth"; import CredentialsProvider from "next-auth/providers/credentials"; import { PrismaAdapter } from "@next-auth/prisma-adapter"; import prisma from "@/app/libs/prismadb"; export const authOptions: AuthOptions = { adapter: PrismaAdapter(prisma), providers: [ CredentialsProvider({ name: "credentials", credentials: { email: { label: "email", type: "text" }, password: { label: "password", type: "password" }, }, async authorize(credentials) { if (!credentials?.email || !credentials?.password) { throw new Error("Invalid credentials 1"); } const user = await prisma.user.findUnique({ where: { email: credentials.email, }, }); const isCorrectPassword = user ? await bcrypt.compare( credentials.password, user.hashedPassword || "" ) : false; if (!isCorrectPassword) { throw new Error("Invalid credentials 2"); } return user; }, }), ], debug: process.env.NODE_ENV === "development", session: { strategy: "jwt", }, secret: process.env.NEXTAUTH_SECRET, };
- 修改
app/api/auth/[...nextauth]/route.ts:
import NextAuth from "next-auth"; import { authOptions } from "@/app/libs/auth"; const handler = NextAuth(authOptions); export { handler as GET, handler as POST };
方法2:使用局部变量(临时方案)
如果不需要复用authOptions,可以将其改为路由文件内的局部变量,不导出:
import bcrypt from "bcrypt"; import NextAuth, { AuthOptions } from "next-auth"; import CredentialsProvider from "next-auth/providers/credentials"; import { PrismaAdapter } from "@next-auth/prisma-adapter"; import prisma from "@/app/libs/prismadb"; // 不导出authOptions,作为局部变量 const authOptions: AuthOptions = { adapter: PrismaAdapter(prisma), providers: [ CredentialsProvider({ name: "credentials", credentials: { email: { label: "email", type: "text" }, password: { label: "password", type: "password" }, }, async authorize(credentials) { if (!credentials?.email || !credentials?.password) { throw new Error("Invalid credentials 1"); } const user = await prisma.user.findUnique({ where: { email: credentials.email, }, }); const isCorrectPassword = user ? await bcrypt.compare( credentials.password, user.hashedPassword || "" ) : false; if (!isCorrectPassword) { throw new Error("Invalid credentials 2"); } return user; }, }), ], debug: process.env.NODE_ENV === "development", session: { strategy: "jwt", }, secret: process.env.NEXTAUTH_SECRET, }; const handler = NextAuth(authOptions); export { handler as GET, handler as POST };
内容的提问来源于stack exchange,提问作者Jonaskop44
相关产品推荐
相关产品推荐

