Error message
invalid signature
What it means
The token parsed fine and its structure is valid, but the cryptographic check failed: the signature over header.payload does not verify against the key you supplied. Either the key is not the one that signed the token, or the signed bytes changed after signing.
Why it happens
Verifying with the wrong key
commonThe most frequent case by far: the environment holds a different key than the issuer used — a stale public key after rotation, a staging key against production tokens, or a JWKS kid mismatch when the verifier pins one key while the issuer signs with another.
A hand-rolled signer signed the wrong bytes
occasionalCode that builds tokens by hand must sign the ASCII bytes of base64url(header).base64url(payload) with SHA-256 and emit the raw 64-byte r‖s that RFC 7518 requires for ES256. Signing the JSON itself, standard-base64 segments, or a SHA-384 digest under an ES256 header produces a well-formed signature that never verifies. (A DER-encoded signature is rejected before the check, with ""ES256" signatures must be "64" bytes", and a PEM mangled by an environment variable is not accepted as a key at all: it fails with "secretOrPublicKey must be an asymmetric key when using ES256".)
The token was modified after signing
occasionalA proxy, logger or client re-serialized the payload JSON or changed characters in it. Even one changed character in header or payload invalidates the signature — that is the whole point of signing. (Base64 padding added to a segment fails one step earlier, with "invalid token"; a token cut short fails with "jwt malformed" or a signature-length error.)
How to fix it
- 1.
Fingerprint the key on both sides
Hash the public key the verifier actually loaded and compare it with the issuer's. Different fingerprints mean a stale or wrong key — the most frequent cause by far — and end the search in one step.
js import { createHash, createPublicKey } from "node:crypto"; // Run on the verifier, and on the issuer with its signing key: the outputs must match. const spki = createPublicKey(process.env.JWT_PUBLIC_KEY).export({ type: "spki", format: "der" }); console.log(createHash("sha256").update(spki).digest("hex")); - 2.
Match the token's kid to the verifying key
Decode the header without verifying and compare its kid with the key you hold. If the issuer publishes a JWKS, resolve the key by kid instead of hard-coding one.
js const { header } = jwt.decode(token, { complete: true }); console.log(header.kid, header.alg); // compare with your JWKS / configured key - 3.
Let the debugger name the mismatch
Paste the token and public key into the JWT debugger: it verifies ES256/ES384/ES512 locally and distinguishes wrong key, tampered payload and malformed signature encoding instead of a bare boolean.