getToken({ req }) gives null even though the user is signed in
I 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.
Fix it fast
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)2Match what you see, then jump to its fix
- 1
Works locally, returns null after deployThe secret is missing or differs between environmentsFix - 2
The 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
The cookie header from the first step is empty or has no session cookieThe request holds no session cookieFix
- 1
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-string3Verify
Sign in again so a new token is created, then call the route.
curl -s -b cookies.txt https://example.com/api/whoamiConfirmed by authjs.dev
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
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/whoami2Fix
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
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
Web apps
getSession finds no user inside a Next.js API route
An API route that checks the session answers 401 for a signed-in user
getSession is meant for the browser and does an extra fetch when used on the server.
3 causes, each with a fixWeb apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fix