Error message
jwt malformed
What it means
jsonwebtoken's jwt.verify() could not even parse the token: the string does not split into exactly three segments separated by dots. The signature was never checked — the string you passed is not structurally a JWT. (A string that does have three segments but whose header is not base64url JSON fails one step later, with "invalid token".)
Why it happens
The Authorization header was split the wrong way
commonThe Authorization header value is "Bearer eyJ...", and the code kept the wrong half of it: auth.split(" ")[0] hands jwt.verify() the scheme "Bearer" itself, which has no dots at all. Passing the whole "Bearer eyJhbGci..." string fails differently — it still splits into three segments, so jsonwebtoken reports "invalid token" when the first one does not decode.
The wrong variable reached verify()
commonA string that is not a token ended up where the token should be: "undefined" or "null" from interpolating a missing value (a client sending "Bearer null" because nothing was stored, a misspelled req.headers.authorisation, a missing cookie), "[object Object]" from stringifying the decoded payload instead of passing the raw token, or a user id. A real undefined, null or empty string is caught one check earlier, with "jwt must be provided".
The token was truncated in storage or transit
occasionalA VARCHAR(255) database column, a log line limit, or a copy-paste that lost the tail can cut the token so that fewer than three segments survive. ES256 tokens run ~200+ characters; RS256 tokens are far longer.
It is not a JWT at all
occasionalSome OAuth providers issue opaque access tokens (random strings) that are not JWTs. Passing a Google/GitHub opaque access_token to jwt.verify() fails here — only id_token (OpenID Connect) is guaranteed to be a JWT.
How to fix it
- 1.
Strip the scheme prefix before verifying
Take only the token part of the Authorization header, and guard against a missing header so you fail with a clear 401 instead of "jwt malformed".
js const auth = req.headers.authorization ?? ""; const [scheme, token] = auth.split(" "); if (scheme !== "Bearer" || !token) { return res.status(401).json({ error: "missing bearer token" }); } jwt.verify(token, publicKey, { algorithms: ["ES256"] }); - 2.
Log the raw value and count its segments
One log line tells you whether you are dealing with a prefix, a truncation or the wrong variable. A JWS has exactly 3 segments; a JWE has 5; anything else is not a token.
js console.log(JSON.stringify(token).slice(0, 80)); console.log("segments:", String(token).split(".").length); // must be 3 - 3.
Check what the token actually contains
Paste the string into the JWT debugger below: it decodes the header and claims locally and tells you precisely which segment is broken — or that the string is not a JWT at all.