Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

NextAuth cannot read its own session cookie and logs a JWT session error

Your error

[next-auth][error][JWT_SESSION_ERROR] https://next-auth.js.org/errors#jwt_session_error decryption operation failed3 more lines it can show

Fix it fast

Most common cause, 1 of 3

The secret changed while browsers still hold the old session cookie

For a single developer, signing in again is enough.

Fix
// auth.ts (Auth.js 5)
import NextAuth from "next-auth"

export const { handlers, auth, signIn, signOut } = NextAuth({
  secret: [process.env.AUTH_SECRET_NEW!, process.env.AUTH_SECRET_OLD!],
  providers: [],
})
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

JWT_SESSION_ERROR in next-auth 4, and JWTSessionError in Auth.js 5, mean the session cookie was encrypted with a different secret than the server holds now. A changed secret, no fixed secret in development, or two apps sharing one cookie cause it. Set one AUTH_SECRET everywhere, sign in again, and rotate with a secret array. Applies to next-auth 4.24.15 and 5.0.0-beta.32.

  • Applies to next-auth 4.24.15, next-auth 5.0.0-beta.32 (@auth/core 0.41.3), Next.js 16.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

On every machine and environment that serves the app, print a short hash of the secret (never the value itself). An empty value points to cause 2, fingerprints that differ between instances point to cause 3, and matching ones mean an old cookie from before a change (cause 1).

Run this first
node -e "const s=process.env.AUTH_SECRET||process.env.NEXTAUTH_SECRET||''; console.log(s.length, require('crypto').createHash('sha256').update(s).digest('hex').slice(0,12))"

Then find the line you see in this list and open its fix:

  1. 1The secret changed while browsers still hold the old session cookieThe error starts right after a deploy or an edit to the env file, and each browser stops showing it once the user signs in again
  2. 2next-auth 4 runs in development with no secret, so the key moves with your configNo secret is set, the log also shows a NO_SECRET warning, and the decryption error appears after you edit the NextAuth options, such as adding a provider
  3. 3Two apps or instances with different secrets receive the same cookieThe fingerprints from the first step differ between servers, or a second Auth.js app runs on another localhost port

What does the error look like?

  • [next-auth][error][JWT_SESSION_ERROR] https://next-auth.js.org/errors#jwt_session_error decryption operation failed
  • JWEDecryptionFailed: decryption operation failed
  • [auth][error] JWTSessionError: Read more at https://errors.authjs.dev#jwtsessionerror
  • no matching decryption secret

Cause 1 of 3

The secret changed while browsers still hold the old session cookie

You seeThe error starts right after a deploy or an edit to the env file, and each browser stops showing it once the user signs in again

Check

JWT_SESSION_ERROR with JWEDecryptionFailed appears when NEXTAUTH_SECRET or AUTH_SECRET was replaced but a session made under the old value is still active. The next-auth error reference names this exact case. Auth.js 5 also deletes the unreadable cookie when it logs JWTSessionError, so the message usually shows once per visitor.

Check
grep -n "AUTH_SECRET\|NEXTAUTH_SECRET" .env .env.local .env.production 2>/dev/null | cut -d= -f1

Fix

For a single developer, signing in again is enough. On a live site, keep the old value working during the switch: Auth.js accepts an array of secrets, encrypts new sessions with the first one and tries each entry when decrypting. Remove the old entry after existing sessions have expired.

Fix
// auth.ts (Auth.js 5)
import NextAuth from "next-auth"

export const { handlers, auth, signIn, signOut } = NextAuth({
  secret: [process.env.AUTH_SECRET_NEW!, process.env.AUTH_SECRET_OLD!],
  providers: [],
})

Verify

Load a page as a user who signed in before the change. The server log stays quiet and the session still holds the user.

Verify
npm run dev 2>&1 | grep -i "jwt\|session"

Confirmed by next-auth.js.org

Did this fix it?

Cause 2 of 3

next-auth 4 runs in development with no secret, so the key moves with your config

You seeNo secret is set, the log also shows a NO_SECRET warning, and the decryption error appears after you edit the NextAuth options, such as adding a provider

Check

Without NEXTAUTH_SECRET in development, next-auth 4 derives its secret from a hash of all configuration options, OAuth client IDs included. Any edit to those options gives a new key, and cookies made before the edit can no longer be opened. The options page warns about this in its tip on the secret setting.

Check
node -e "console.log(Boolean(process.env.NEXTAUTH_SECRET || process.env.AUTH_SECRET))"

Fix

Generate a fixed value once and keep it in .env.local. The Auth.js CLI writes it for you; openssl gives the same kind of random string. Restart the dev server so the process reads it, then sign in again.

Fix
npx auth secret
# or, by hand:
openssl rand -base64 32
# .env.local
AUTH_SECRET=paste-the-generated-value-here

Verify

Change a harmless option in the auth config, restart, and reload. You stay signed in and no JWT_SESSION_ERROR is printed.

Verify
npm run dev

Confirmed by next-auth.js.org

Did this fix it?

Cause 3 of 3

Two apps or instances with different secrets receive the same cookie

You seeThe fingerprints from the first step differ between servers, or a second Auth.js app runs on another localhost port

Check

Auth.js describes JWTSessionError as a decode failure caused by a misconfigured secret or a malformed token. One common setup that produces it: several instances behind a load balancer, each with its own AUTH_SECRET, so a cookie made by one is rejected by the next. Another is local work on two apps, because cookies are not isolated by port: the app on port 3001 receives the cookie set on port 3000 and cannot decrypt it.

Check
# run inside each server, container or app folder and compare the output
node -e "const s=process.env.AUTH_SECRET||''; console.log(require('crypto').createHash('sha256').update(s).digest('hex').slice(0,12))"

Fix

Give every instance of one app the same AUTH_SECRET from a single place, such as the host's environment settings, and redeploy them together. For two different apps on localhost, clear the localhost cookies when you switch between them, or set a distinct session cookie name in one app with the cookies option.

Fix
# same value on every instance of the app
AUTH_SECRET=one-shared-value-from-your-secret-store

Verify

Every instance prints the same fingerprint, and after a fresh sign-in the service log stays free of JWTSessionError while requests move between servers.

Verify
journalctl -u my-next-app --since "-15min" | grep -c JWTSessionError

Confirmed by authjs.dev

Did this fix it?

How do I stop it coming back?

  • Store AUTH_SECRET in one secret store and inject it into every environment, instead of generating a value per server.
  • Plan secret rotation with the array form: add the new value first, deploy, and drop the old one only after the session max age has passed.
  • Set a secret in development too, even on next-auth 4, so editing providers never logs you out.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.