Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Auth.js · Const

MongoDBAdapter

MongoDBAdapter connects Auth.js to MongoDB by turning a MongoDB client source into an Auth.js Adapter. It supports direct clients and custom client factories, with optional connection-close handling.

How it works

MongoDBAdapter accepts a MongoDB client source and returns an Auth.js Adapter. The client may be a MongoClient, a promise, or a function; the function form supports custom connection management together with onClose.

Syntax

function MongoDBAdapter(client, options): Adapter

Parameters

NameTypeDescription
clientMongoClient | Promise<MongoClient> | () => MongoClient | Promise<MongoClient>MongoDB client, a promise resolving to a MongoClient, or a function returning either form.
optionsobjectAdapter options, including optional onClose connection-cleanup behavior.

Returns

Adapter

Key points

  • MongoDBAdapter is the official MongoDB integration for Auth.js and NextAuth.js.
  • The factory takes a client and options and returns an Auth.js Adapter.
  • The client argument represents the MongoDB client source used by the adapter.
  • A promise resolving to a connected MongoClient is supported as an alternative client input.
  • A function client input supports more complex connection strategies.
  • The onClose option is a callback for managing MongoDB client shutdown.

Common mistakes

  • Manage the MongoDB connection yourself; the adapter does not open or close connections automatically.
  • Prefer passing a non-connected MongoClient rather than a promise resolving to a connected client, because failed connections can otherwise produce unhandled promise rejections.

Example: Use MongoDBAdapter with Auth.js

import NextAuth from "next-auth"
import { MongoDBAdapter } from "@auth/mongodb-adapter"
import client from "./lib/db"

export const { handlers, auth } = NextAuth({
  adapter: MongoDBAdapter(client),
  providers: [],
})

Pass a MongoClient to MongoDBAdapter, then assign the resulting adapter to Auth.js configuration.

When to use it

MongoDB-backed authentication

An Auth.js application needs users, accounts, sessions, or verification data persisted in MongoDB.

Custom connection lifecycle

An application must control when and how MongoDB connections are created or reused.

Managed client shutdown

A deployment needs explicit cleanup for a MongoDB client.

Interview questions

QWhat does MongoDBAdapter do?

MongoDBAdapter is the official MongoDB adapter for Auth.js. It receives a MongoDB client source and produces an Adapter that Auth.js can use for database-backed authentication data.

Follow-up: What client forms can it accept? A MongoClient, a promise resolving to one, or a function returning one; the function form is useful for custom connection strategies.

QHow is MongoDBAdapter configured?

Pass the returned adapter through Auth.js configuration using the adapter option. The usual form is MongoDBAdapter(client), where client is imported from the application's database module.

Follow-up: Why might the client be supplied as a function? It enables more customized connection behavior, especially when paired with the onClose option.

QWhat connection-management concern applies?

The adapter does not manage connections automatically, so the supplied MongoClient must be handled by the application. The documented recommendation is to provide a non-connected MongoClient instance.

Follow-up: When is a promise acceptable? It can resolve to a connected MongoClient, but the documentation marks that approach as not recommended because connection failures can cause unhandled rejections.

  • Adapter
  • MongoClient
  • onClose
  • @auth/mongodb-adapter

Source: Auth.js | Mongodb Adapter. Checked 2026-10-15.