JWT Explained Step-by-Step With A Banking Example: JWT Authentication Best Explanation - Spring Boot Tutorial in HINDI
JWT Explained Step-by-Step With A Banking Example: JWT Authentication Best Explanation - Spring Boot Tutorial in HINDI
Imagine you're using an online banking app to manage your account. Here's how JWT fits into
this process:
Real Life:
You open the banking app, enter your username and password.
JWT Action:
Real Life:
The app safely stores the token, like keeping your ATM receipt in your wallet for reference.
JWT Action:
The token is stored securely, often in:
Real Life:
You want to check your account balance.
JWT Action:
Real Life:
You decide to transfer ₹10,000 to a friend.
JWT Action:
● The app sends the transfer details (amount, recipient) to the server, along with the JWT.
● The server verifies the JWT as before and checks if you’re allowed to perform the
transaction.
● If valid, the server processes the transfer and responds with a confirmation.
Real Life:
After some time, your session at the ATM ends, and you need to re-enter your PIN.
JWT Action:
Real Life:
Imagine the ATM offers an option to extend your session without logging in again.
JWT Action:
● The app uses a refresh token (stored securely) to get a new JWT when the old one
expires.
● This allows you to stay logged in for longer without frequently entering your credentials.
1. Secure Transactions:
○ The token’s signature ensures no one has tampered with your identity.
○ Tokens are valid only for a short time, minimizing risks.
2. Convenience:
○ You don’t need to log in for every action.
○ The token proves your identity for the duration of its validity.
3. Stateless Server:
○ The server doesn’t store session data for every user.
○ The token carries all necessary information, making it efficient.
Summary
JWT (JSON Web Token) is like a digital ID card for secure communication between a client
(e.g., your chat app) and a server. It helps the server confirm a user's identity without storing
session details on the server.
How JWT Works (Step-by-Step)
1. User Logs In
● For every subsequent request, the user includes the JWT (e.g., in the Authorization
header).
● Authenticate users.
● Secure real-time communication via WebSocket (e.g., using Socket.IO).
Debugging JWT
1. What is JWT, and why did you choose it for your chat app?
● JWT (JSON Web Token) is a compact, self-contained token used for securely sharing
information between parties.
● Why JWT in Chat App?
○ It provides stateless authentication, so the server doesn’t need to store session
data.
○ It’s easy to implement and ensures secure communication between the frontend
and backend.
2. Explain the structure of a JWT. What are its three parts, and what do they do?
● Header: Contains metadata like the token type (JWT) and signing algorithm (e.g.,
HS256).
● Payload: Contains user information (claims), such as user ID or roles. It’s not encrypted,
so don’t store sensitive data here.
● Signature: Ensures the token isn’t tampered with by hashing the header and payload
with a secret key.
4. What signing algorithm did you use for your JWTs? Why?
5. How did you implement authentication using JWT in your MERN stack?
1. Login: When the user logs in, the server creates a JWT with their information and sends
it back.
2. Store Token: The token is stored in the browser (e.g., cookies).
3. Token in Requests: For each API request, the frontend sends the token to the server
for validation.
4. Protect Routes: Middleware on the backend checks the token to grant access.
● HttpOnly Cookies:
○ It’s more secure as the token isn’t accessible to JavaScript, preventing XSS
(Cross-Site Scripting).
● Why Not LocalStorage?
○ Tokens in LocalStorage can be exposed to XSS attacks since they are accessible
via JavaScript.
9. Follow-up: What happens if the token is invalid or expired?
● If the token is invalid: The server responds with a 400 status code (Invalid Token).
● If the token is expired: The user is asked to log in again or use a refresh token to get a
new JWT.
10. What claims did you include in the JWT payload? Why?
● Claims I included:
○ userId: Identifies the user.
○ role: Determines user permissions.
○ exp: Token expiration time (for security).
● Why?: These are essential for verifying the user’s identity and controlling access.