ecdsa.com
jsonwebtoken (Node.js)JWT libraries

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

How to fix it

  1. 1.

    Fix escaped newlines in the key

    If the key comes from an environment variable, normalize it before use, and print the first line to make sure it is the PEM you expect.

    js
    const pem = process.env.JWT_PUBLIC_KEY.replace(/\\n/g, "\n");
    console.log(pem.split("\n")[0]); // -----BEGIN PUBLIC KEY-----
    jwt.verify(token, pem, { algorithms: ["ES256"] });
  2. 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. 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.

Related errors

← Browse the full signature error database