@auth/mongodb-adapter
Official MongoDB adapter for Auth.js / NextAuth.js.
Installationβ
- npm
- Yarn
- pnpm
npm install @auth/mongodb-adapter mongodb
yarn add @auth/mongodb-adapter mongodb
pnpm add @auth/mongodb-adapter mongodb
MongoDBAdapter()β
MongoDBAdapter(
client
,options
):Adapter
Setupβ
The MongoDB adapter does not handle connections automatically, so you will have to make sure that you pass the Adapter a MongoClient
that is connected already. Below you can see an example how to do this.
Add the MongoDB clientβ
// This approach is taken from https://github.com/vercel/next.js/tree/canary/examples/with-mongodb
import { MongoClient } from "mongodb"
if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"')
}
const uri = process.env.MONGODB_URI
const options = {}
let client
let clientPromise: Promise<MongoClient>
if (process.env.NODE_ENV === "development") {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
if (!global._mongoClientPromise) {
client = new MongoClient(uri, options)
global._mongoClientPromise = client.connect()
}
clientPromise = global._mongoClientPromise
} else {
// In production mode, it's best to not use a global variable.
client = new MongoClient(uri, options)
clientPromise = client.connect()
}
// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise
Configure Auth.jsβ
import NextAuth from "next-auth"
import { MongoDBAdapter } from "@auth/mongodb-adapter"
import clientPromise from "../../../lib/mongodb"
// For more information on each option (and a full list of options) go to
// https://authjs.dev/reference/providers/oauth
export default NextAuth({
adapter: MongoDBAdapter(clientPromise),
...
})
Parametersβ
βͺ client: Promise
< MongoClient
> | () => Promise
< MongoClient
>
The MongoDB client. You can either pass a promise that resolves to a MongoClient
or a function that returns a promise that resolves to a MongoClient
.
Using a function that returns a Promise<MongoClient>
could be useful in serverless environments, particularly when combined with options.onClose
, to efficiently handle database connections and address challenges with persistence, container reuse, and connection closure.
These functions enable either straightforward open-close database connections or more complex caching and connection reuse strategies.
βͺ options: MongoDBAdapterOptions
= {}
Returnsβ
Adapter
MongoDBAdapterOptionsβ
This is the interface of the MongoDB adapter options.
Propertiesβ
collectionsβ
collections?:
object
The name of the MongoDB collections.
Type declarationβ
Accountsβ
Accounts?:
string
Sessionsβ
Sessions?:
string
Usersβ
Users?:
string
VerificationTokensβ
VerificationTokens?:
string
databaseNameβ
databaseName?:
string
The name you want to give to the MongoDB database
onCloseβ
onClose?: (
client
) =>Promise
<void
>
Callback function for managing the closing of the MongoDB client.
This could be useful in serverless environments, especially when client
is provided as a function returning Promise<MongoClient>
, not just a simple promise.
It allows for more sophisticated management of database connections,
addressing persistence, container reuse, and connection closure issues.
Parametersβ
βͺ client: MongoClient
Returnsβ
Promise
< void
>