Closed
Description
Adapter type
@auth/mongodb-adapter
Environment
System:
OS: Windows 11 10.0.26100
CPU: (8) x64 AMD Ryzen 3 5300U with Radeon Graphics
Memory: 957.40 MB / 15.33 GB
Binaries:
Node: 22.16.0 - C:\Program Files\nodejs\node.EXE
Yarn: 3.4.1 - ~\AppData\Roaming\npm\yarn.CMD
npm: 11.3.0 - C:\Program Files\nodejs\npm.CMD
pnpm: 10.12.4 - ~\AppData\Local\pnpm\pnpm.CMD
bun: 1.1.2 - ~\.bun\bin\bun.EXE
Browsers:
Edge: Chromium (131.0.2903.70)
Internet Explorer: 11.0.26100.1882
npmPackages:
@auth/mongodb-adapter: ^3.10.0 => 3.10.0
next: 15.3.5 => 15.3.5
next-auth: 5.0.0-beta.29 => 5.0.0-beta.29
react: ^19.0.0 => 19.1.0
Reproduction URL
i have not created
Describe the issue
I'm using NextAuth@beta
with the following config:
strategy: "database"
in thesession
optionMongoDBAdapter
properly connected with a working MongoDB instanceCredentialsProvider
for email/password login
However, after successful login:
- The browser receives a JWT-based session token (encrypted JWE format), instead of a
session-token
referencing a DB record - The MongoDB
sessions
collection remains empty - Debug logs show calls like
adapter_getSessionAndUser
using a JWT instead of a DB ID
I've tried the following:
- Clearing cookies
- Rechecking the adapter config
- Enabling debug logging
- Ensuring
authorize()
returns{ id, name, email }
Still, the session is never persisted to MongoDB and the cookie format indicates fallback to JWT mode even though "database"
is configured.
How to reproduce
🧪 How to Reproduce
-
Clone the reproduction repository (linked above).
-
Install dependencies:
pnpm install
-
Set environment variables in
.env.local
:AUTH_SECRET=some-secret MONGODB_URI=mongodb+srv://<user>:<pass>@<cluster>.mongodb.net/<db>?retryWrites=true&w=majority API_URL=http://localhost:3000 AUTH_GOOGLE_ID=your-google-id AUTH_GOOGLE_SECRET=your-google-secret
-
Start the dev server:
pnpm dev
-
Visit
/login
and use the Credentials Provider (email/password) to sign in. -
Check:
- Browser cookie:
session-token
is an encrypted JWT - MongoDB:
sessions
collection is empty - Debug log:
adapter_getSessionAndUser
tries to use JWT
- Browser cookie:
Expected behavior
- After signing in using the Credentials Provider, the session token should be stored as a random string (UUID or similar) in a cookie (e.g.,
session-token
). - This token should correspond to a session document stored in the MongoDB
sessions
collection usingMongoDBAdapter
. - The cookie should not contain a JWT or encrypted JWE-style token.
- When accessing a protected route,
NextAuth
should fetch the session from the database viaadapter.getSessionAndUser()
using thesessionToken
.