DEV Community

Raisha Sultana
Raisha Sultana

Posted on

Authentication Using JWT Explained (Simple and Clear Guide for Beginners

Authentication is one of the most important parts of modern web development. Almost every application today needs authentication. Whether it is a social media app, an e-commerce website, or a dashboard, authentication helps verify users and protect data.

One of the most popular authentication methods today is JWT authentication.

In this article, you will learn what JWT is, how JWT authentication works, why it is used, and how to implement it in a simple way. This guide uses easy and natural English so beginners can understand clearly.


What Is Authentication?

Authentication is the process of verifying the identity of a user.

For example, when you log in using your email and password, the system checks if your credentials are correct. If they are correct, you are allowed to access your account.

Authentication answers this question:

"Is this user really who they claim to be?"

Without authentication, anyone could access private data.


What Is JWT?

JWT stands for JSON Web Token.

It is a secure way to send information between a client and a server.

A JWT is a string that contains encoded information about the user.

Example of a JWT:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Enter fullscreen mode Exit fullscreen mode

This token is used to identify the user after login.

Instead of logging in again and again, the user sends the JWT with each request.


Why JWT Is Used

JWT is used because it is:

  • Secure
  • Fast
  • Stateless
  • Easy to use
  • Widely supported

JWT allows the server to verify users without storing session data.

This makes applications faster and more scalable.


How JWT Authentication Works

JWT authentication works in simple steps.

Step 1: User Logs In

The user enters email and password.

Example:

Email: user@gmail.com

Password: 123456
Enter fullscreen mode Exit fullscreen mode

The client sends this data to the server.


Step 2: Server Verifies User

The server checks if the email and password are correct.

If correct, the server creates a JWT.


Step 3: Server Sends JWT to Client

The server sends the JWT to the client.

Example response:

json
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
Enter fullscreen mode Exit fullscreen mode

The client stores this token.

Usually stored in:

Local storage

Cookies

Step 4: Client Sends JWT with Requests

When the user makes a request, the client sends the token.

Example:

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Enter fullscreen mode Exit fullscreen mode

Step 5: Server Verifies JWT

The server checks if the token is valid.

If valid, the server allows access.

If invalid, access is denied.

Structure of JWT

A JWT has three parts:

Header.Payload.Signature

Enter fullscreen mode Exit fullscreen mode

Example:

xxxxx.yyyyy.zzzzz

Enter fullscreen mode Exit fullscreen mode
  1. Header

The header contains information about the token.

Example:

{
"alg": "HS256",
"typ": "JWT"
}

It tells the algorithm and token type.

  1. Payload

The payload contains user data.

Example:

{
"userId": 123,
"email": "user@gmail.com"
}

This data is used to identify the user.

  1. Signature

The signature protects the token from tampering.

It ensures the token is secure.

If someone changes the token, the signature becomes invalid.

JWT Authentication Example Using Node.js

Here is a simple example using Node.js and jsonwebtoken.

Install package
npm install jsonwebtoken

Create token
const jwt = require("jsonwebtoken");

const user = {
id: 1,
email: "user@gmail.com"
};

const token = jwt.sign(user, "secretKey", { expiresIn: "1h" });

console.log(token);

This creates a JWT.

Verify token
const jwt = require("jsonwebtoken");

const token = "your_token_here";

try {
const decoded = jwt.verify(token, "secretKey");
console.log(decoded);
} catch (error) {
console.log("Invalid token");
}

This verifies the token.

How JWT Is Used in Real Applications

JWT is used in:

Login systems

APIs

Mobile apps

Web apps

Dashboards

Example flow:

User logs in

Server creates JWT

Client stores JWT

Client sends JWT with requests

Server verifies JWT

This keeps the user authenticated.

Advantages of JWT Authentication

JWT has many advantages.

  1. Stateless

The server does not store session data.

This improves performance.

  1. Fast

JWT verification is fast.

No database lookup required.

  1. Secure

JWT uses cryptographic signatures.

This protects data.

  1. Scalable

JWT works well for large applications.

No session storage needed.

  1. Works Across Platforms

JWT works with:

Web apps

Mobile apps

APIs

JWT vs Session Authentication
Feature JWT Session
Storage Client Server
Speed Fast Slower
Scalability High Limited
Stateless Yes No

JWT is better for modern applications.

Where JWT Is Stored

JWT is usually stored in:

Local storage

Cookies

Example:

localStorage.setItem("token", token);

Sending JWT in Requests

Example using fetch:

fetch("/api/data", {
headers: {
Authorization: "Bearer " + token
}
});

This sends the token to the server.

Common JWT Mistakes to Avoid

  1. Storing sensitive data in payload

Do not store passwords.

JWT payload is readable.

  1. Using weak secret keys

Use strong secret keys.

Example:

mySuperSecretKey123!

  1. Not setting expiration

Always set expiration.

Example:

expiresIn: "1h"

When Should You Use JWT

Use JWT when building:

Login systems

REST APIs

Single page applications

Mobile applications

JWT is ideal for modern authentication.

Real-World Example

When you log in to websites like:

GitHub

Facebook

Dashboards

They often use JWT authentication.

It allows secure and fast authentication.

Conclusion

JWT authentication is one of the most important concepts in modern web development.

JWT allows secure communication between client and server.

Key points to remember:

JWT stands for JSON Web Token

It is used for authentication

It contains header, payload, and signature

It is secure and fast

It is widely used in modern applications

If you are learning web development, understanding JWT authentication is essential.

It will help you build secure login systems and real-world applications.

JWT is simple, powerful, and widely used in the industry.

Top comments (0)