Advanced Topic 38 of 51 Web Development
JWT
Signed tokens carrying claims between parties.
How it works
A JSON Web Token is a compact, URL-safe token with three Base64URL parts: a header naming the algorithm, a payload of claims such as subject, expiry and roles, and a signature made with a secret or private key. The server verifies the signature to trust the claims without a database lookup, which makes JWTs convenient for stateless APIs and single sign-on. Access tokens are short-lived and are paired with refresh tokens.
Step-by-step walkthrough
- A JWT has three base64 parts.
- The payload carries claims such as user id and expiry.
- The server signs header and payload with a secret or private key.
- Clients send the token in each request.
- The server verifies the signature, so it needs no session lookup, but cannot revoke easily.
Where it is used
API authentication, microservice-to-microservice calls, OpenID Connect ID tokens and mobile app sessions use JWTs.
Common pitfall
The payload is only encoded, not encrypted, so never put secrets in it. Accepting the none algorithm or confusing symmetric and asymmetric keys leads to forgery, so enforce expected algorithms. Revocation is hard because tokens stay valid until they expire. Storing them in local storage exposes them to XSS. Keep lifetimes short, validate every claim and rotate keys.
Where it fits in the course
JWT is topic 38 of 51 in Web Development, rated Advanced. It follows Authentication Flows. The next topic is OAuth 2.0.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Web Development quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Web Development quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is JWT?
Where do people use JWT?
What is the most common mistake with JWT?
What should I learn before this topic?
How can I practice it?
Is JWT hard to learn?
How long does it take to learn JWT?
Which topics are related to JWT?
What should I study after JWT?
Related topics
- Authentication Flows (Web Development)
- OAuth 2.0 (Web Development)
- Express and Middleware (Web Development)
- CORS (Web Development)