JWT stands for JSON Web Token
It is used for secure authentication and authorization π
In simple words
JWT is a digital token
That proves the user is already logged in π§
Without JWT β
β’ User must login again and again
β’ APIs cannot verify identity
β’ Sessions become harder to manage
π§ What JWT Contains
β’ Header β token type and algorithm
β’ Payload β user data
β’ Signature β verifies token integrity
π How JWT Works
User logs in π€
Server creates a JWT πͺ
JWT is sent to client π©
Client sends JWT with every request π
Server verifies JWT before giving access β
π Real World Usage
β’ Login systems
β’ Protected APIs
β’ Role based access
β’ Mobile and web applications
π» Example: Creating JWT in Node
const jwt = require("jsonwebtoken");
const user = { id: 1, role: "admin" };
const token = jwt.sign(user, "secret_key", {
expiresIn: "1h"
});
console.log(token);