jwt.verify throws 'invalid signature' for a token that looks fine
JsonWebTokenError: invalid signatureShort answer
A JWT signature is computed from the header, payload and secret. 'invalid signature' means the secret used to verify is not byte-for-byte the one used to sign. Usual reasons: two different secrets (often an unset environment variable) or one side treating the secret as base64 while the other uses it as plain text.
Fix it fast
1Find out which cause you have
Print a short fingerprint of the secret on the signing side and on the verifying side. Never print the secret itself. If the two values differ, you have found the cause.
node -e "console.log(require('crypto').createHash('sha256').update(process.env.JWT_SECRET ?? '').digest('hex').slice(0, 8))"2Match what you see, then jump to its fix
What does the error look like?
JsonWebTokenError: invalid signatureJsonWebTokenError (name of the error object)
Cause 1 of 2
Signing and verifying use different secrets
1Check
The fingerprint above differs between the two services, or is the hash of an empty string because the variable is not set in one environment. For HMAC algorithms the secret must be identical on both sides.
echo ${JWT_SECRET:+set}2Fix
Put the same secret in the environment of every service that signs or verifies, then restart them. Tokens signed with the old secret stay invalid and must be reissued.
const token = jwt.sign({ sub: userId }, process.env.JWT_SECRET);
const payload = jwt.verify(token, process.env.JWT_SECRET);3Verify
Sign and verify in one process with the real secret.
node -e "const j=require('jsonwebtoken');const t=j.sign({a:1},process.env.JWT_SECRET);console.log(j.verify(t,process.env.JWT_SECRET).a)"Confirmed by github.com
Cause 2 of 2
The secret is base64 on one side and plain text on the other
1Check
jsonwebtoken reads a string secret as utf-8 text. Other tools can hold the same secret base64-encoded and decode it before signing, so the signing key is the original bytes.
node -e "console.log(Buffer.from(process.env.JWT_SECRET, 'base64').length, process.env.JWT_SECRET.length)"2Fix
If the token was signed with the decoded bytes, pass a Buffer to verify. If your own code signed it, keep the plain string on both sides.
jwt.verify(token, Buffer.from(process.env.JWT_SECRET, 'base64'));3Verify
Verify a token you know is good.
node -e "console.log(require('jsonwebtoken').decode(process.argv[1], {complete:true}).header)" "$TOKEN"Confirmed by github.com
How do I stop it coming back?
- Keep one source of truth for the secret (a secrets manager or one env file) and load it the same way in every service.
- Fail at startup when the secret is missing instead of signing with undefined.
- Rotate secrets on a planned date and expect old tokens to stop verifying.
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
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix