Understand what is JWT(JSON Web Token) and how JWT authentication work. It also includes pros and cons of using JWT tokens instead of cookie-based authentication.
Frontendgeek
Last Updated Sep 22, 2025
JWT(JSON Web Token) is currently becoming the standard of web authorization, where the token carries all the required information along with the token and on server the information is decoded by the server using key. It is very important to understand JWT Token, JWT decode and pros and cons.
A JSON Web Token (JWT) is a compact, URL-safe way of securely transmitting information between client and server in most of the general cases.
You can understand this with an analogy of a digital boarding pass ✈️ — once you get it after logging in, you keep showing it to prove your identity until it expires.
A JWT Token has three parts:
A JWT (JSON Web Token) is made up of three parts: Each part is Base64URL encoded and separated by dots.
<header>.<payload>.<signature>
Here’s a sample JWT string (shortened for readability):
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwicm9sZSI6ImFkbWluIiwiZXhwIjoxNzE2MjM5MDIyfQ.
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
When decoded, it has three JSON objects: JWT Decode Applications
alg
: Algorithm used to sign the token (HS256 = HMAC + SHA-256).
typ
: Type of token (JWT).
{
"alg": "HS256",
"typ": "JWT"
}
This is the part of the token that carries the actual payload data to the server.
{
"sub": "1234567890",
"name": "John Doe",
"role": "admin",
"exp": 1716239022
}
sub
: Subject (usually the user ID).
name
: User’s name.
role
: Role of the user (admin, user, etc.).
exp
: Expiry timestamp (Unix epoch).
The server uses this signature to verify that the token hasn’t been altered.
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
Here is the end-to-end JWT authentication flow
1️⃣ User logs in with valid credentials.
2️⃣ Server validates and generates a JWT (with header, payload, and signature).
3️⃣ This token is sent back to the client and stored (usually in localStorage or cookies).
4️⃣ For every request, the client attaches the token in the "Authorization" header.
5️⃣ Server verifies the signature to ensure it hasn’t been tampered with.
6️⃣ If valid → ✅ access granted. If not → ❌ access denied.
let's understand the important advantages which JWT token provides instead of cookie-based authentication, and in which cases JWT token is not a better choice
✅ Stateless
No need for the server to store session data.
Works perfectly with microservices and distributed systems.
✅ Scalable
Easy to use with APIs, SPAs (React/Next.js), and mobile apps.
Doesn’t rely solely on browser cookies.
✅ Compact & Fast
Base64 encoded → small enough to send in HTTP headers.
Less overhead compared to storing session IDs.
✅ Flexible Payload
❌ Difficult to revoke
❌ Size bloat
❌ Security risks
❌ Expiry handling
Advertisement
Advertisement
Vijay Sai Krishna vsuri
Last Updated Aug 21, 2025
A Quick guide about popstate event in JavaScript, If you’ve ever hit the back button in your browser and wondered how your Single-Page Application knows which view to render, this guide is for you.
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Aug 3, 2025
Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.
Kirtesh Bansal
Last Updated Aug 31, 2025
A beginner-friendly guide to understanding call, apply, and bind methods in JavaScript, along with step-by-step call, apply and bind polyfill implementations that are often asked in interviews.
Anuj Sharma
Last Updated Sep 14, 2025
Learn hoisting in JavaScript with clear examples and explanations. Understand variable hoisting in JavaScript, function hoisting in JavaScript, and how the temporal dead zone affects hoisting in JS.
Anuj Sharma
Last Updated Aug 3, 2025
Understand what all steps are involved in the Critical Rendering Path (CRP) and how optimization of different steps can improve the overall web-vitals of a web application
Subscribe to FrontendGeek Hub for the frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2024 FrontendGeek. All rights reserved