英文:
next-auth error: 'Adapter' is not assignable to type 'Adapter | undefined'
问题
我正在按照这份文档进行操作:https://authjs.dev/reference/adapter/mongodb 来使用 next-auth。以下是相关的代码部分:
import NextAuth from "next-auth"
import { MongoDBAdapter } from "@auth/mongodb-adapter"
import clientPromise from "../../../lib/mongodb"
...
export default NextAuth({
providers,
adapter: MongoDBAdapter(clientPromise),
})
我收到了以下错误信息,对我来说没有意义:
Type 'Adapter' is not assignable to type 'Adapter | undefined'.
Type 'Adapter' is not assignable to type 'DefaultAdapter & { createVerificationToken: (verificationToken: VerificationToken) => Awaitable<VerificationToken | null | undefined>; useVerificationToken: (params: { ...; }) => Awaitable<...>; }'.
Type 'Adapter' is not assignable to type 'DefaultAdapter'.
Types of property 'createUser' are incompatible.
Type '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.
Type 'undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.ts(2322)
types.d.ts(106, 5): The expected type comes from property 'adapter' which is declared here on type 'AuthOptions'
(property) AuthOptions.adapter?: Adapter | undefined
You can use the adapter option to pass in your database adapter.
Required: No
英文:
I'm following this documentation: https://authjs.dev/reference/adapter/mongodb to use next-auth. This is the relevant code:
import NextAuth from "next-auth"
import { MongoDBAdapter } from "@auth/mongodb-adapter"
import clientPromise from "../../../lib/mongodb"
...
export default NextAuth({
providers,
adapter: MongoDBAdapter(clientPromise),
})
I get the following error, which makes no sense to me:
Type 'Adapter' is not assignable to type 'Adapter | undefined'.
Type 'Adapter' is not assignable to type 'DefaultAdapter & { createVerificationToken: (verificationToken: VerificationToken) => Awaitable<VerificationToken | null | undefined>; useVerificationToken: (params: { ...; }) => Awaitable<...>; }'.
Type 'Adapter' is not assignable to type 'DefaultAdapter'.
Types of property 'createUser' are incompatible.
Type '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.
Type 'undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.ts(2322)
types.d.ts(106, 5): The expected type comes from property 'adapter' which is declared here on type 'AuthOptions'
(property) AuthOptions.adapter?: Adapter | undefined
You can use the adapter option to pass in your database adapter.
Required: No
答案1
得分: 17
我今天也遇到了同样的问题。我从 @auth/mongodb-adapter 中导入了 MongoDBAdapter,但实际上需要从 next-auth 适配器中导入。
我运行了:
npm install @next-auth/mongodb-adapter
然后将我的导入更改为:
import { MongoDBAdapter } from "@next-auth/mongodb-adapter";
我不知道实际问题是什么,只知道安装了正确的适配器并更改了导入后,问题就解决了。
希望对你有所帮助!
英文:
I ran into the same issue today. I was importing MongoDBAdapter from @auth/mongodb-adapter, but actually needed to import from the next-auth adapter.
I ran:
npm install @next-auth/mongodb-adapter
Then changed my import to:
import { MongoDBAdapter } from "@next-auth/mongodb-adapter";
I have no clue what the actual issue is, just that installing the right adapter and changing the import fixed it for me.
Hope this helps!
答案2
得分: 8
我遇到了与 @auth/firebase-adapter
相同类型的错误。
类型错误:类型 'Adapter' 不能赋值给类型 'Adapter | undefined'。
类型 'Adapter' 不能赋值给类型 'DefaultAdapter & { createVerificationToken: (verificationToken: VerificationToken) => Awaitable<VerificationToken | null | undefined>; useVerificationToken: (params: { ...; }) => Awaitable<...>; }'。
类型 'Adapter' 不能赋值给类型 'DefaultAdapter'。
属性 'createUser' 的类型不兼容。
类型 '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined' 不能赋值给类型 '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'。
类型 'undefined' 不能赋值给类型 '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'
首先,从 next-auth/adapters
导入类型 Adapter
。
import type { Adapter } from "next-auth/adapters";
并更改 NextAuthOptions。
import type { Adapter } from "next-auth/adapters";
export const authOptions: NextAuthOptions = {,
adapter: FirestoreAdapter(firestore) as Adapter,
...
};
英文:
I got the same type error with @auth/firebase-adapter
.
ype error: Type 'Adapter' is not assignable to type 'Adapter | undefined'.
Type 'Adapter' is not assignable to type 'DefaultAdapter & { createVerificationToken: (verificationToken: VerificationToken) => Awaitable<VerificationToken | null | undefined>; useVerificationToken: (params: { ...; }) => Awaitable<...>; }'.
Type 'Adapter' is not assignable to type 'DefaultAdapter'.
Types of property 'createUser' are incompatible.
Type '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.
Type 'undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'
First, import type Adapter
from next-auth/adapters
.
import type { Adapter } from "next-auth/adapters";
and change NextAuthOptions.
import type { Adapter } from "next-auth/adapters";
export const authOptions: NextAuthOptions = {,
adapter: FirestoreAdapter(firestore) as Adapter,
...
};
答案3
得分: 1
我曾经在使用Redis/Upstash适配器,并遇到了相同的问题。
import { UpstashRedisAdapter } from '@next-auth/upstash-redis-adapter'
在Auth.js中,他们一直在使用这个
import { UpstashRedisAdapter } from "@auth/upstash-redis-adapter";
但是随着Next 13.3的更新,这成了一个问题。
英文:
I was working with Redis/upstash adapter, and had the same issue.
import { UpstashRedisAdapter } from '@next-auth/upstash-redis-adapter'
In Auth.js they have been using this
import { UpstashRedisAdapter } from "@auth/upstash-redis-adapter";
But with the Next 13.3 updates, this has been an issue
答案4
得分: 0
这似乎已经成为这个过渡中的常见问题,因为 NextAuth 适配器文档链接引用 AuthJS 适配器文档作为“最新文档”。对我来说,Supabase 适配器也出现了同样的问题,链接的文档中要求安装:
npm install @supabase/supabase-js @auth/supabase-adapter
通过编辑导入语句从 @auth/supabase-adapter
更改为 @next-auth/supabase-adapter
并相应地更新我的依赖项来解决了错误。
如果有人有帮助,我在 Fedora 操作系统上进行开发时实际上没有遇到任何类型的问题,只有在运行 Ubuntu 的 Windows 笔记本电脑上的 WSL/2 时才出现了问题。我没有时间诊断原因,也许这是 Windows 的一个问题,但欢迎分享相关知识 =)
英文:
I'm thinking that this has become a common issue on this transition as NextAuth adapter doc links ref the AuthJS adapter docs as "the up-to-date documentation". Same issue occurred for me with the Supabase adapter where the linked docs state to install:
npm install @supabase/supabase-js @auth/supabase-adapter
Error was cleared by editing the import statement from @auth/supabase-adapter
to @next-auth/supabase-adapter
and updating my dependencies accordingly.
In case it helps anyone, there were actually no Typing issues for me while dev'ing on a fedora os and only popped up on my windows laptop running ubuntu on wsl/2. Don't have the time to diagnose the why, maybe its a windows thing, but feel free to chime in with knowledge =)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论