Skip to content

NextAuth uses JWT despite session.strategy = "database" and MongoDBAdapter #13105

Closed
@Shani-Sinojiya

Description

@Shani-Sinojiya

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 the session option
  • MongoDBAdapter properly connected with a working MongoDB instance
  • CredentialsProvider 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

  1. Clone the reproduction repository (linked above).

  2. Install dependencies:

    pnpm install
  3. 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
    
  4. Start the dev server:

    pnpm dev
  5. Visit /login and use the Credentials Provider (email/password) to sign in.

  6. Check:

    • Browser cookie: session-token is an encrypted JWT
    • MongoDB: sessions collection is empty
    • Debug log: adapter_getSessionAndUser tries to use JWT

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 using MongoDBAdapter.
  • 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 via adapter.getSessionAndUser() using the sessionToken.

Metadata

Metadata

Assignees

No one assigned

    Labels

    adaptersChanges related to the core code concerning database adaptersbugSomething isn't workinginvalid reproductionThe issue did not have a detectable valid reproduction URLtriageUnseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions