What is JWT (JSON Web Token), and how is it used in MERN authentication?

The Best Full Stack MERN Training Institute in Hyderabad with Live Internship Program

If you're looking to build a successful career in web development, Quality Thought is the top destination in Hyderabad for Full Stack MERN (MongoDB, Express.js, React, Node.js) training. Known for its industry-oriented curriculum and expert trainers, Quality Thought equips students with the skills needed to become job-ready full stack developers.

Our MERN Stack training program covers everything from front-end to back-end development. You'll start with MongoDB, a powerful NoSQL database, move on to Express.js and Node.js for back-end development, and master React for building dynamic and responsive user interfaces. The course structure is designed to offer a perfect blend of theory and hands-on practice, ensuring that students gain real-world coding experience.

What sets Quality Thought apart is our Live Internship Program, which allows students to work on real-time industry projects. This not only strengthens technical skills but also builds confidence to face real development challenges. Students get direct mentorship from industry experts, and experience the workflow of actual development environments, making them industry-ready.

We also provide complete placement assistance, resume building sessions, mock interviews, and soft skills training to help our students land high-paying jobs in top tech companies.

Join Quality Thought and transform yourself into a skilled MERN Stack Developer. Whether you're a fresher or a professional looking to upskill, this course is your gateway to exciting career opportunities in full stack development.

Enroll now and take the first step toward becoming a certified MERN stack professional with hands-on internship experience!

🔹 What is JWT?

  • A token format (string) used for securely transmitting user identity and claims.

  • Consists of three parts (Base64-encoded):

    1. Header → type of token (JWT) + algorithm (HS256).

    2. Payload → user info / claims (e.g., id, role).

    3. Signature → ensures integrity (created with secret key).

🔹 How JWT Works in MERN Authentication

1. User Login

  • User sends login credentials (email/password) to Express backend.

  • Backend verifies credentials against MongoDB.

  • If valid, backend creates a JWT containing user details (like id) and signs it with a secret key.

  • Token is sent back to frontend (React).

2. Frontend Stores Token

  • React app receives the token.

  • Stores it in localStorage or HTTP-only cookie.

localStorage.setItem("token", token);

3. Authenticated Requests

  • For protected routes, React sends token in the Authorization header.

fetch("/api/protected", { headers: { "Authorization": "Bearer " + localStorage.getItem("token") } });

4. Backend Verifies Token

  • Express middleware checks the JWT before giving access.

const jwt = require("jsonwebtoken"); function authenticateToken(req, res, next) { const authHeader = req.headers["authorization"]; const token = authHeader && authHeader.split(" ")[1]; if (!token) return res.sendStatus(401); jwt.verify(token, "secretKey", (err, user) => { if (err) return res.sendStatus(403); req.user = user; // attach user info to request next(); }); }

5. Access Granted

  • If token is valid → user can access protected route.

  • If token is expired/invalid → server responds with 401 Unauthorized.

🔹 Benefits of JWT in MERN

  • Stateless → no need to store sessions on the server.

  • Scalable → works across multiple servers (distributed systems).

  • Secure → tamper-proof with signature.

  • Flexible → can include user roles, permissions, and expiry.

✅ In short:
JWT in MERN authentication is used to securely pass a user’s identity between React (frontend) and Express (backend), allowing protected API access without storing session data on the server.

Read More :

Visit  Quality Thought Training Institute in Hyderabad      

Comments

Popular posts from this blog

Describe a project you built using MERN stack.

What are mocks and spies in testing?

What is the difference between process.nextTick() and setImmediate()?