Backend & Node.js · APIs, data and failure

How does JWT authentication work and what are its risks?

MediumjwtauthtokensUpdated Oct 2026
Quiz this track

Short answer

A JWT is a signed set of claims in three base64url parts. The server verifies the signature and claims, then treats the token as proof of identity. Risks include weak verification, long lifetimes, unsafe storage and revocation difficulty.

A JWT has a header, a payload and a signature. The payload is encoded, not encrypted, so anyone holding the token can read it. The signature lets the issuer's key prove the claims were not modified. The server must verify the algorithm, signature, issuer, audience and expiry.

js
import jwt from "jsonwebtoken";

const token = jwt.sign(
  { sub: user.id, role: "admin" },
  privateKey,
  {
    algorithm: "RS256",
    expiresIn: "15m",
    issuer: "api.example.com",
    audience: "web",
  },
);

const claims = jwt.verify(token, publicKey, {
  algorithms: ["RS256"],
  issuer: "api.example.com",
  audience: "web",
});
Pin the algorithm and check issuer and audience. Never trust the algorithm named in the token header.

The risks

  • Accepting the alg header without allowlisting enables algorithm confusion attacks
  • A stolen token works until it expires, so short access token lifetimes matter
  • Revocation is hard without a denylist, token version or short-lived refresh flow
  • Storing tokens in localStorage exposes them to XSS; httpOnly cookies reduce that risk but need CSRF protection
  • Putting secrets or personal data in the payload leaks them, because the payload is readable
  • Clock skew can cause valid tokens to be rejected; allow a small leeway

A production setup often uses a short-lived access token and a longer-lived refresh token stored in an httpOnly cookie. Refresh tokens can be rotated and revoked server-side. For high-risk actions, require re-authentication rather than trusting a long-lived claim.

JWT is a token format, not an authentication protocol. OAuth2 and OpenID Connect define how tokens are issued, scoped and refreshed. Do not build a login protocol from JWTs alone.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Is a JWT encrypted?
  2. 2How do you revoke a JWT before it expires?
  3. 3Where should a browser store a token?
  4. 4Why must the server pin the verification algorithm?

Say it in the interview

A JWT is a signed token with readable claims. The server verifies the signature and checks issuer, audience and expiry, then treats it as identity. Because it is stateless, revocation is the hard part; I keep access tokens short-lived and use rotated refresh tokens for longer sessions. I pin the algorithm, never store secrets in the payload, and avoid localStorage for tokens in a browser.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Backend & Node.js mock interview and get scored feedback on depth, clarity and confidence.