Back to Blog

Understanding JWT Tokens for Beginners

2025-08-03 FrameSite Labs

Authentication is one of the most important responsibilities in modern web development. JSON Web Tokens, or JWTs, have become the dominant standard for stateless authentication. If you have ever logged into an application and seen a long string of characters passed back and forth, you have likely encountered a JWT.

This guide explains what JWTs are, how they are structured, and the security best practices you need to follow.

What is a JWT?

A JWT is a compact, self-contained way to transmit information between two parties as a JSON object. Because it is digitally signed, the information can be verified and trusted.

JWTs are commonly used for:

  • Authentication tokens after login.
  • Information exchange between microservices.
  • Stateless session management (no server-side storage required).

The Structure of a JWT

A JWT looks like this:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Despite being a single string, it contains three distinct parts separated by dots (.):

1. Header

The header typically contains two parts: the type of token (JWT) and the signing algorithm (HS256, RS256, etc.). It is Base64URL encoded.

2. Payload

The payload contains the claims — statements about the user and additional metadata. Common claims include:

  • sub (subject): The user ID.
  • iat (issued at): Timestamp of creation.
  • exp (expiration): Timestamp when the token expires.
  • iss (issuer): Who issued the token.

3. Signature

The signature is created by combining the encoded header, encoded payload, a secret key, and the algorithm specified in the header. This ensures that the token has not been tampered with.

How JWT Authentication Works

  1. The user submits credentials to the server.
  2. The server validates the credentials and generates a JWT signed with a secret.
  3. The client stores the JWT (typically in memory or a secure HTTP-only cookie).
  4. On subsequent requests, the client sends the JWT in the Authorization header.
  5. The server verifies the signature and extracts the user identity from the payload.

Security Best Practices

Keep Your Secret Safe

The signing secret is the foundation of trust. If an attacker obtains it, they can forge valid tokens. Store secrets in environment variables, never in source code.

Use Short Expiration Times

Set a reasonable exp claim — typically 15 minutes to 1 hour for access tokens. Use refresh tokens to maintain sessions without keeping long-lived access tokens in circulation.

Validate the Signature

Always verify the signature before trusting the payload. Skipping validation defeats the entire purpose of using JWTs.

Use HTTPS

JWTs are sent in headers. Without HTTPS, they can be intercepted by man-in-the-middle attacks. Never transmit JWTs over plain HTTP.

Do Not Store Sensitive Data in the Payload

The payload is Base64URL encoded, not encrypted. Anyone can decode it. Never store passwords, credit card numbers, or other sensitive information inside a JWT payload.

Common JWT Mistakes

  • Ignoring expiration: Failing to check the exp claim can lead to indefinite access for stolen tokens.
  • Weak secrets: Short, guessable secrets make brute-force attacks trivial.
  • Mixing authentication and authorization: A JWT proves identity. It does not replace role-based access control logic on the server.

Debugging JWTs

When a JWT is not working as expected, debugging starts with decoding the header and payload to inspect the claims. Our free JWT Decoder lets you paste any JWT and instantly view its decoded contents — no server upload required.

Summary

  • A JWT has three parts: Header, Payload, and Signature.
  • It enables stateless authentication without server-side sessions.
  • Always verify the signature and expiration.
  • Never store sensitive data in the payload.
  • Keep your signing secret secure and use HTTPS.

JWTs are a powerful tool, but like any tool, they are only secure when used correctly. Understanding their structure and limitations is the first step toward building robust authentication systems.