‘Adapter’ 不可分配给类型 ‘Adapter | undefined’ 的错误。

huangapple go评论46阅读模式
英文:

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 &quot;next-auth&quot;
import { MongoDBAdapter } from &quot;@auth/mongodb-adapter&quot;
import clientPromise from &quot;../../../lib/mongodb&quot;

...

export default NextAuth({
    providers,
    adapter: MongoDBAdapter(clientPromise),
})

I get the following error, which makes no sense to me:

Type &#39;Adapter&#39; is not assignable to type &#39;Adapter | undefined&#39;.
  Type &#39;Adapter&#39; is not assignable to type &#39;DefaultAdapter &amp; { createVerificationToken: (verificationToken: VerificationToken) =&gt; Awaitable&lt;VerificationToken | null | undefined&gt;; useVerificationToken: (params: { ...; }) =&gt; Awaitable&lt;...&gt;; }&#39;.
    Type &#39;Adapter&#39; is not assignable to type &#39;DefaultAdapter&#39;.
      Types of property &#39;createUser&#39; are incompatible.
        Type &#39;((user: Omit&lt;AdapterUser, &quot;id&quot;&gt;) =&gt; Awaitable&lt;AdapterUser&gt;) | undefined&#39; is not assignable to type &#39;(user: Omit&lt;AdapterUser, &quot;id&quot;&gt;) =&gt; Awaitable&lt;AdapterUser&gt;&#39;.
          Type &#39;undefined&#39; is not assignable to type &#39;(user: Omit&lt;AdapterUser, &quot;id&quot;&gt;) =&gt; Awaitable&lt;AdapterUser&gt;&#39;.ts(2322)
types.d.ts(106, 5): The expected type comes from property &#39;adapter&#39; which is declared here on type &#39;AuthOptions&#39;
(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 &quot;@next-auth/mongodb-adapter&quot;;

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 相同类型的错误。

类型错误:类型 &#39;Adapter&#39; 不能赋值给类型 &#39;Adapter | undefined&#39;。
  类型 &#39;Adapter&#39; 不能赋值给类型 &#39;DefaultAdapter &amp; { createVerificationToken: (verificationToken: VerificationToken) =&gt; Awaitable&lt;VerificationToken | null | undefined&gt;; useVerificationToken: (params: { ...; }) =&gt; Awaitable&lt;...&gt;; }&#39;。
    类型 &#39;Adapter&#39; 不能赋值给类型 &#39;DefaultAdapter&#39;。
      属性 &#39;createUser&#39; 的类型不兼容。
        类型 &#39;((user: Omit&lt;AdapterUser, &quot;id&quot;&gt;) =&gt; Awaitable&lt;AdapterUser&gt;) | undefined&#39; 不能赋值给类型 &#39;(user: Omit&lt;AdapterUser, &quot;id&quot;&gt;) =&gt; Awaitable&lt;AdapterUser&gt;&#39;。
          类型 &#39;undefined&#39; 不能赋值给类型 &#39;(user: Omit&lt;AdapterUser, &quot;id&quot;&gt;) =&gt; Awaitable&lt;AdapterUser&gt;&#39;

首先,从 next-auth/adapters 导入类型 Adapter

import type { Adapter } from &quot;next-auth/adapters&quot;;

并更改 NextAuthOptions。

import type { Adapter } from &quot;next-auth/adapters&quot;;

export const authOptions: NextAuthOptions = {,
  adapter: FirestoreAdapter(firestore) as Adapter,
  ...
};
英文:

I got the same type error with @auth/firebase-adapter.

ype error: Type &#39;Adapter&#39; is not assignable to type &#39;Adapter | undefined&#39;.
  Type &#39;Adapter&#39; is not assignable to type &#39;DefaultAdapter &amp; { createVerificationToken: (verificationToken: VerificationToken) =&gt; Awaitable&lt;VerificationToken | null | undefined&gt;; useVerificationToken: (params: { ...; }) =&gt; Awaitable&lt;...&gt;; }&#39;.
    Type &#39;Adapter&#39; is not assignable to type &#39;DefaultAdapter&#39;.
      Types of property &#39;createUser&#39; are incompatible.
        Type &#39;((user: Omit&lt;AdapterUser, &quot;id&quot;&gt;) =&gt; Awaitable&lt;AdapterUser&gt;) | undefined&#39; is not assignable to type &#39;(user: Omit&lt;AdapterUser, &quot;id&quot;&gt;) =&gt; Awaitable&lt;AdapterUser&gt;&#39;.
          Type &#39;undefined&#39; is not assignable to type &#39;(user: Omit&lt;AdapterUser, &quot;id&quot;&gt;) =&gt; Awaitable&lt;AdapterUser&gt;&#39;

First, import type Adapter from next-auth/adapters.

import type { Adapter } from &quot;next-auth/adapters&quot;;

and change NextAuthOptions.

import type { Adapter } from &quot;next-auth/adapters&quot;;

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 &#39;@next-auth/upstash-redis-adapter&#39; 

In Auth.js they have been using this

import { UpstashRedisAdapter } from &quot;@auth/upstash-redis-adapter&quot;;

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 =)

huangapple
  • 本文由 发表于 2023年6月19日 12:27:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定