Next.js 14中NextAuth与Prisma Adapter类型不兼容问题求助
Next.js 14 + NextAuth 类型错误求助
开发Next.js 14项目时,在app路由下配置NextAuth遇到无法解决的类型错误,相关信息如下:
我的路由文件代码(./app/api/[...nextauth]/route.js)
// ./app/api/[...nextauth]/route.js import NextAuth from "next-auth/next"; import CredentialsProvider from "next-auth/providers/credentials"; import bcrypt from "bcrypt"; import { PrismaAdapter } from "@auth/prisma-adapter"; import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient; export const authOptions = { adapter: PrismaAdapter(prisma), providers: [ CredentialsProvider({ name: "credentials", credentials: { email: { label: "Email", type: "email" }, password: { label: "Password", type: "password" } }, async authorize(credentials) { // check to see if email and password are valid if(!credentials || !credentials.email || !credentials.password) return null; // Check if the user exists const user = await prisma.user.findUnique({ where: { email: credentials.email } }); if(!user) return null; // Check if passwords match (if user exists) const passwordsMatch = user.hashedPassword && await bcrypt.compare(credentials.password, user.hashedPassword); if(!passwordsMatch) return null; // Return user object if everything is valid return user; } }) ], session: { strategy: 'jwt' as const // Explicitly type as 'jwt' }, secret: process.env.NEXTAUTH_SECRET, debug: process.env.NODE_ENV === "development", }; export const handler = NextAuth(authOptions) export { handler as GET, handler as POST }
初始运行错误
Argument of type '{ adapter: Adapter; providers: CredentialsConfig<{ email: { label: string; type: string; }; password: { label: string; type: string; }; }>[]; session: { strategy: "jwt"; }; secret: string | undefined; debug: boolean; }' is not assignable to parameter of type 'AuthOptions'. The types of 'adapter.createUser' are incompatible between these types. Type '((user: AdapterUser) => Awaitable<AdapterUser>) | undefined' is not assignable to type '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined'. Type '(user: AdapterUser) => Awaitable<AdapterUser>' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'. Types of parameters 'user' and 'user' are incompatible. Property 'id' is missing in type 'Omit<AdapterUser, "id">' but required in type 'AdapterUser'.
尝试添加类型注解后的新错误
将export const authOptions = {修改为export const authOptions: AuthOptions = {后,错误变为:
Type 'import("c:/<path>@auth/core/adapters").Adapter' is not assignable to type 'import("c:/<path>/node_modules/next-auth/adapters").Adapter'. Types of property 'createUser' are incompatible. Type '((user: AdapterUser) => Awaitable<AdapterUser>) | undefined' is not assignable to type '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined'. Type '(user: AdapterUser) => Awaitable<AdapterUser>' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'. Types of parameters 'user' and 'user' are incompatible. Property 'id' is missing in type 'Omit<AdapterUser, "id">' but required in type 'AdapterUser'.
目前完全不清楚问题的成因和解决方法,恳请帮助。
内容的提问来源于stack exchange,提问作者Dimitri Mikhalev
相关产品推荐
相关产品推荐

