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


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 parameterValue
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 parameterValue
TRecord<string, unknown>
Parameters
ParameterType
objectRecord<string, any>
Returns

T & { _id: ObjectId; }


MongoDBAdapter()

MongoDBAdapter(client, options): Adapter

Parameters

ParameterType
clientPromise<MongoClient>
optionsMongoDBAdapterOptions

Returns

Adapter

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