I am getting a typescript error in my next auth config when I try to add a MongoDB adapter. I have a mongoDB and my idea is to create users using next auth, and then login with the credentials provider.
I am using Next Auth V5.
Here is my next auth config file:
import type { NextAuthConfig } from 'next-auth'
import Credentials from 'next-auth/providers/credentials'
import { MongoDBAdapter } from "@auth/mongodb-adapter"
import mongoClientPromise from './app/lib/mongodb'
export default {
providers: [
Credentials({
async authorize(credentials) {
const user = { name: 'admin', email: '[email protected]', password: '123456' }
if (credentials.user === user.name && credentials.password === user.password) return user
console.log('Invalid credentials')
return null
},
}),
],
adapter: MongoDBAdapter(mongoClientPromise)
} satisfies NextAuthConfig
The error comes in the ‘adapter’ key and this is the message:
** Type ‘AdapterAccount’ is not assignable to type ‘Promise | Awaitable<AdapterAccount | null | undefined>’.
Type ‘AdapterAccount’ is missing the following properties from type ‘Promise’: then, catch, finally, [Symbol.toStringTag]**
Any ideas on how to solve it?
I have been looking at different tutorials, and I stick to them precisely (for example, this one in Medium: https://blog.stackademic.com/authentication-in-next-js-with-auth-js-nextauth-5-b74e3ae18ab8
or even using the official documentation: https://authjs.dev/reference/adapter/mongodb
but the error persists
UPDATE:
I was able to solve it by doing this:
import type { Adapter } from "@auth/core/adapters"
Then adding as it follows in the adapter key:
adapter : <Adapter>MongoDBAdapter(mongoClientPromise)
documentation does not mention but maybe you should be creating a type declaration file:
// globals.d.ts
declare namespace NodeJS {
interface Global {
_mongoClientPromise: Promise<MongoClient>;
}
}
Please post an actual answer if you have solved the issue instead of adding it to the question itself (it’s harder for people to find it).