Tokenize and Secure: A Deep Dive into JWTs for Node.js

In today's digital world, security is a top priority for any web application. One of the most popular methods for securing APIs is using JSON Web Tokens (JWT). In this blog, we'll explore JWT token authentication in Node.js, including how to generate tokens and use them to protect your routes. Let's dive in!

What is JWT?

JWT, or JSON Web Token, is an open standard for securely transmitting information between parties as a JSON object. It is compact, readable, and digitally signed using a secret or a public/private key pair. JWTs are commonly used for authentication and information exchange.

Why Use JWT?

  • Security: JWTs are signed, making them tamper-proof.

  • Compactness: Being a compact token, JWTs are easy to transfer through URLs, POST parameters, or inside HTTP headers.

  • Self-contained: JWTs contain all the necessary information about the user, eliminating the need to query the database multiple times.

Steps to Implement JWT Authentication in Node.js

  1. Setting Up the Project

  2. Installing Required Packages

  3. Creating the Authentication Routes

  4. Generating JWT Tokens

  5. Protecting Routes with Middleware

Step 1: Setting Up the Project

First, create a new directory for your project and initialize a Node.js application.

shCopy codemkdir jwt-authentication
cd jwt-authentication
npm init -y

Step 2: Installing Required Packages

Next, install the necessary packages: Express for handling routes, Bcrypt for hashing passwords, JSONWebToken for creating and verifying tokens, and Mongoose for MongoDB interaction.

shCopy codenpm install express bcryptjs jsonwebtoken mongoose

Step 3: Creating the Authentication Routes

Create a basic Express server and define the routes for user registration and login.

jsCopy codeconst express = require('express');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');

const app = express();
app.use(express.json());

// Connect to MongoDB
mongoose.connect('mongodb://localhost/jwt-auth', { useNewUrlParser: true, useUnifiedTopology: true });

// User schema and model
const userSchema = new mongoose.Schema({
  username: String,
  email: String,
  password: String
});

const User = mongoose.model('User', userSchema);

// Registration route
app.post('/register', async (req, res) => {
  const { username, email, password } = req.body;
  const hashedPassword = await bcrypt.hash(password, 10);
  const user = new User({ username, email, password: hashedPassword });
  await user.save();
  res.status(201).send('User registered');
});

// Login route
app.post('/login', async (req, res) => {
  const { email, password } = req.body;
  const user = await User.findOne({ email });
  if (!user) return res.status(400).send('Invalid credentials');

  const validPassword = await bcrypt.compare(password, user.password);
  if (!validPassword) return res.status(400).send('Invalid credentials');

  const token = jwt.sign({ _id: user._id }, 'secretKey');
  res.header('auth-token', token).send(token);
});

app.listen(3000, () => console.log('Server running on port 3000'));

Step 4: Generating JWT Tokens

In the login route, after verifying the user's credentials, we generate a JWT token. The jsonwebtoken package provides a sign method to create a token.

jsCopy codeconst token = jwt.sign({ _id: user._id }, 'secretKey');

Here, we are signing the token with the user's ID and a secret key. In a real application, make sure to store your secret key securely.

Step 5: Protecting Routes with Middleware

Create a middleware function to protect your routes. This middleware will verify the token and allow access only if the token is valid.

jsCopy codeconst verifyToken = (req, res, next) => {
  const token = req.header('auth-token');
  if (!token) return res.status(401).send('Access Denied');

  try {
    const verified = jwt.verify(token, 'secretKey');
    req.user = verified;
    next();
  } catch (err) {
    res.status(400).send('Invalid Token');
  }
};

// Protected route
app.get('/protected', verifyToken, (req, res) => {
  res.send('This is a protected route');
});

Conclusion

JWT token authentication is a powerful way to secure your Node.js applications. By following these steps, you can implement a robust authentication system that protects your API endpoints. Remember to keep your secret key safe and consider using environment variables for configuration.

Feel free to extend this example to include more features like token expiration, refresh tokens, and role-based access control. Happy coding!

Example Project Structure

bashCopy codejwt-authentication/
│
├── node_modules/
├── package.json
├── server.js
└── .env

Complete Code Example

Here's the complete code for your reference:

jsCopy codeconst express = require('express');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');

const app = express();
app.use(express.json());

mongoose.connect('mongodb://localhost/jwt-auth', { useNewUrlParser: true, useUnifiedTopology: true });

const userSchema = new mongoose.Schema({
  username: String,
  email: String,
  password: String
});

const User = mongoose.model('User', userSchema);

app.post('/register', async (req, res) => {
  const { username, email, password } = req.body;
  const hashedPassword = await bcrypt.hash(password, 10);
  const user = new User({ username, email, password: hashedPassword });
  await user.save();
  res.status(201).send('User registered');
});

app.post('/login', async (req, res) => {
  const { email, password } = req.body;
  const user = await User.findOne({ email });
  if (!user) return res.status(400).send('Invalid credentials');

  const validPassword = await bcrypt.compare(password, user.password);
  if (!validPassword) return res.status(400).send('Invalid credentials');

  const token = jwt.sign({ _id: user._id }, 'secretKey');
  res.header('auth-token', token).send(token);
});

const verifyToken = (req, res, next) => {
  const token = req.header('auth-token');
  if (!token) return res.status(401).send('Access Denied');

  try {
    const verified = jwt.verify(token, 'secretKey');
    req.user = verified;
    next();
  } catch (err) {
    res.status(400).send('Invalid Token');
  }
};

app.get('/protected', verifyToken, (req, res) => {
  res.send('This is a protected route');
});

app.listen(3000, () => console.log('Server running on port 3000'));
0
Subscribe to my newsletter

Read articles from Rohan Shrivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rohan Shrivastava
Rohan Shrivastava

Hi, I'm Rohan, a B.Tech graduate in Computer Science (Batch 2022) with expertise in web development (HTML, CSS, JavaScript, Bootstrap, PHP, XAMPP). My journey expanded with certifications and intensive training at Infosys, covering DBMS, Java, SQL, Ansible, and networking. I've successfully delivered projects, including a dynamic e-commerce site and an Inventory Management System using Java. My proactive approach is reflected in certifications and contributions to open-source projects on GitHub. Recognized for excellence at Infosys, I bring a blend of technical proficiency and adaptability. Eager to leverage my skills and contribute to innovative projects, I'm excited about exploring new opportunities for hands-on experiences. Let's connect and explore how my skills align with your organization's goals.