Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

next-auth error: 'Adapter' is not assignable to type 'Adapter | undefined'

Tags:

next-auth

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
like image 932
Emanuele Paolini Avatar asked Dec 10 '25 15:12

Emanuele Paolini


2 Answers

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!

like image 59
Rgamer43 Avatar answered Dec 12 '25 03:12

Rgamer43


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,
  ...
};
like image 24
Ko Tin Avatar answered Dec 12 '25 03:12

Ko Tin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!