Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
API reference@auth/mongodb-adapter

@auth/mongodb-adapter

Official MongoDB adapter for Auth.js / NextAuth.js.

Installation

npm install @auth/mongodb-adapter mongodb

MongoDBAdapterOptions

This is the interface of the MongoDB adapter options.

Properties

collections?

optional collections: {
  Accounts: string;
  Sessions: string;
  Users: string;
  VerificationTokens: string;
};

The name of the MongoDB collections.

Accounts?
optional Accounts: string;
Sessions?
optional Sessions: string;
Users?
optional Users: string;
VerificationTokens?
optional VerificationTokens: string;

databaseName?

optional databaseName: string;

The name you want to give to the MongoDB database

onClose()?

optional onClose: (client) => Promise<void>;

Callback function for managing the closing of the MongoDB client. This could be useful when client is provided as a function returning MongoClient. It allows for more customized management of database connections, addressing persistence, container reuse, and connection closure issues.

Parameters
ParameterType
clientMongoClient
Returns

Promise<void>


defaultCollections

const defaultCollections: Required<Required<MongoDBAdapterOptions>["collections"]>;

format

const format: {
  from: T;
  to: T & {
     _id: ObjectId;
    };
};

Type declaration

from()

Takes a MongoDB object and returns a plain old JavaScript object

Type Parameters
Type ParameterDefault type
TRecord<string, unknown>
Parameters
ParameterType
objectRecord<string, any>
Returns

T

to()

Takes a plain old JavaScript object and turns it into a MongoDB object

Type Parameters
Type ParameterDefault type
TRecord<string, unknown>
Parameters
ParameterType
objectRecord<string, any>
Returns

T & { _id: ObjectId; }


MongoDBAdapter()

function MongoDBAdapter(client, options): Adapter

Parameters

ParameterTypeDescription
client| MongoClient | Promise<MongoClient> | () => | MongoClient | Promise<MongoClient>The MongoDB client. The MongoDB team recommends providing a non-connected MongoClient instance to avoid unhandled promise rejections if the client fails to connect. Alternatively, you can also pass: - A promise that resolves to a connected MongoClient (not recommended). - A function, to handle more complex and custom connection strategies. Using a function combined with options.onClose, can be useful when you want a more advanced and customized connection strategy to address challenges related to persistence, container reuse, and connection closure.
optionsMongoDBAdapterOptions-

Returns

Adapter

Auth.js © Balázs Orbán and Team - 2025