How to jwt tokens work

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: JWTs, or JSON Web Tokens, are a secure and compact way to transmit information between parties as a JSON object. They are typically used for authentication and information exchange, consisting of a header, payload, and signature, all encoded in Base64.

Key Facts

Overview

JSON Web Tokens (JWTs) are a popular standard (RFC 7519) for creating access tokens that assert some number of claims. They are a compact, URL-safe means of representing claims to be transferred between two parties. JWTs are commonly used in web applications for authentication and authorization purposes, allowing a server to verify the identity of a user without needing to maintain session state on the server itself. This makes them a crucial component in modern, distributed, and stateless application architectures.

What is a JWT?

A JWT is essentially a string that is composed of three parts, separated by dots (.). These parts are:

  1. Header: The header is a JSON object that typically contains information about the token, such as the type of token (JWT) and the signing algorithm being used (e.g., HMAC SHA256 or RSA). This JSON object is Base64Url encoded.
  2. Payload: The payload is a JSON object that contains the claims. Claims are statements about an entity (typically, the user) and any additional data. There are three types of claims: registered claims, public claims, and private claims. Registered claims are pre-defined and recommended but not mandatory. Public claims are defined by those using JWTs but should be registered in the IANA JSON Web Token Registry or be a URI that contains a collision-resistant identifier. Private claims are custom claims created to share information between parties that agree on their structure. The payload is also Base64Url encoded.
  3. Signature: The signature is created by taking the encoded header, the encoded payload, a secret (for symmetric algorithms) or a private key (for asymmetric algorithms), and signing them using the algorithm specified in the header. The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message wasn't changed along the way.

How JWTs Work in Authentication

The typical flow for using JWTs for authentication is as follows:

  1. User Login: A user logs in to a web application by providing their credentials (e.g., username and password).
  2. Server Verification: The server verifies the user's credentials.
  3. Token Generation: If the credentials are valid, the server generates a JWT. This token contains information about the user (claims) and is signed by the server using a secret key or a private key.
  4. Token Transmission: The server sends the JWT back to the client (e.g., the user's browser).
  5. Client Storage: The client stores the JWT, often in local storage or a cookie.
  6. Subsequent Requests: For subsequent requests to protected resources, the client includes the JWT in the HTTP request's Authorization header, typically in the format 'Bearer [token]'.
  7. Server Verification: The server receives the request, extracts the JWT, and verifies its signature using the same secret key or public key used during generation. If the signature is valid, it means the token hasn't been tampered with and was issued by the server. The server can then trust the claims within the payload and grant access to the requested resource without needing to query a database for user session information.

Benefits of Using JWTs

When to Use JWTs

JWTs are well-suited for scenarios where:

Security Considerations

While JWTs offer security benefits, they are not a silver bullet. It's essential to be aware of potential vulnerabilities:

Sources

  1. JWT.io - The Runtime for JSON Web Tokensfair-use
  2. RFC 7519 - JSON Web Token (JWT)CC-BY-4.0
  3. Using localStorage - Web APIs | MDNCC-BY-SA-2.5

Missing an answer?

Suggest a question and we'll generate an answer for it.