Maaz Zindani

Loading the index…

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

getToken({ req }) gives null even though the user is signed in

ErrorI can't get token with getToken({req})

Short answer

getToken reads the session JWT from the request's cookies or its Authorization header and decrypts it with your secret. It returns null when the secret differs from the one that signed the token, when it looks for the wrong cookie name on HTTPS, or when the request carries no cookies at all.

  • Applies to Next.js 15, next-auth 4, Auth.js 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which cookies actually reach the server code. If no session cookie is listed, the problem is the request, not the secret.

    // in the route or middleware that calls getToken
    console.log(req.headers.get ? req.headers.get('cookie') : req.headers.cookie)
  2. 2Match what you see, then jump to its fix

    1. 1Works locally, returns null after deployThe secret is missing or differs between environmentsFix
    2. 2The cookie header lists a session cookie, but the token is null; the site is served over HTTPSgetToken looks for a different cookie name than the one the browser holdsFix
    3. 3The cookie header from the first step is empty or has no session cookieThe request holds no session cookieFix

What does the error look like?

  • I can't get token with getToken({req})
  • const token = await getToken({ req }) // null

Cause 1 of 3

The secret is missing or differs between environments

1Check

The secret hashes tokens, signs cookies and derives the keys. In production it is required, and AUTH_SECRET is an alias of NEXTAUTH_SECRET. A token made with one value cannot be read with another.

printenv | grep -E '^(AUTH|NEXTAUTH)_SECRET' | sed 's/=.*/=<set>/'

2Fix

Set one secret value for every instance that signs or reads tokens. If you pass it to getToken, pass the same value.

# .env.production
AUTH_SECRET=replace-with-a-long-random-string

3Verify

Sign in again so a new token is created, then call the route.

curl -s -b cookies.txt https://example.com/api/whoami

Confirmed by authjs.dev

Did this fix it?

Cause 2 of 3

getToken looks for a different cookie name than the one the browser holds

1Check

In production the cookie name gets a secure prefix, unless NEXTAUTH_URL starts with http://. If NEXTAUTH_URL is set to an http:// address while the site is served over HTTPS, the names no longer match.

echo "$NEXTAUTH_URL"

2Fix

Make the URL setting match the public HTTPS address, or tell getToken what to look for with secureCookie or cookieName.

import { getToken } from 'next-auth/jwt'

const token = await getToken({
  req,
  secret: process.env.AUTH_SECRET,
  secureCookie: true,
})

3Verify

The token object comes back with your user data.

console.log(token?.sub)

Confirmed by authjs.dev

Did this fix it?

Cause 3 of 3

The request holds no session cookie

1Check

getToken needs the JWT in the request's cookies or in an Authorization header. So the request that reaches your route must carry one of them.

curl -i -b "$COOKIE_HEADER" https://example.com/api/whoami

2Fix

Make the call carry the session cookie, or send the token in an Authorization header. On a server-to-server call, copy the incoming cookie header.

// server-side call that forwards the cookies
await fetch(`${process.env.APP_URL}/api/whoami`, {
  headers: { cookie: req.headers.get('cookie') ?? '' },
})

3Verify

The first-step log now shows the session cookie.

console.log(req.headers.get('cookie'))

Confirmed by authjs.dev

Did this fix it?

How do I stop it coming back?

  • Keep one secret for each environment in your secrets store, never different values per instance.
  • Set NEXTAUTH_URL (or AUTH_URL) to the public HTTPS address in production.
  • Test the sign-in flow on the deployed URL after every proxy or domain change.

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 built on the same technologies:

Related fixes

Next step

Want this handled for good?

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