@auth/mongodb-adapter
Official MongoDB adapter for Auth.js / NextAuth.js.
Installation
npm install @auth/mongodb-adapter mongodbMongoDBAdapterOptions
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 | Promise<MongoClient>.
It allows for more customized management of database connections,
addressing persistence, container reuse, and connection closure issues.
Parameters
| Parameter | Type |
|---|---|
client | MongoClient |
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 parameter | Value |
|---|---|
T | Record<string, unknown> |
Parameters
| Parameter | Type |
|---|---|
object | Record<string, any> |
Returns
T
to()
Takes a plain old JavaScript object and turns it into a MongoDB object
Type parameters
| Type parameter | Value |
|---|---|
T | Record<string, unknown> |
Parameters
| Parameter | Type |
|---|---|
object | Record<string, any> |
Returns
T & {
_id: ObjectId;
}
MongoDBAdapter()
MongoDBAdapter(client, options): AdapterParameters
| Parameter | Type | Description |
|---|---|---|
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 that returns MongoClient | Promise<MongoClient>, 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. |
options | MongoDBAdapterOptions | - |