Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

jwt.verify throws 'invalid signature' for a token that looks fine

ErrorJsonWebTokenError: invalid signature

Short 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.

  • Applies to Node.js 22, jsonwebtoken 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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))"
  2. 2Match what you see, then jump to its fix

    1. 1invalid signature on every token, even a fresh oneSigning and verifying use different secretsFix
    2. 2Token from another library or tool fails, while tokens from your own code verifyThe secret is base64 on one side and plain text on the otherFix

What does the error look like?

  • JsonWebTokenError: invalid signature
  • JsonWebTokenError (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

Did this fix it?

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

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

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