1. Introduction

User Authentication contains various steps, please check out this flowchart to know more. We will be using this flow to build the authentication system in our application.

2. Prerequisites

You should have prior knowledge of javascript basics, nodejs. Knowledge of ES6 syntax is a plus. And, at last nodejs should be installed on your system.

3. Packages Required

You will be needing these following ‘npm’ packages.

express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications

express-validator
To Validate the body data on the server in the express framework, we will be using this library. It’s a server-side data validation library. So, even if a malicious user bypasses the client-side verification, the server-side data validation will catch it and throw an error.

body-parser
It is nodejs middleware for parsing the body data.

bcryptjs
This library will be used to hash the password and then store it to database.This way even app administrators can’t access the account of a user.

jsonwebtokenjsonwebtoken will be used to encrypt our data payload on registration and return a token. We can use that token to authenticate ourselves to secured pages like the dashboard. There would also an option to set the validity of those token, so you can specify how much time that token will last.

mongoose
Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. Mongoose supports both promises and callbacks.

4. Initiate Project

We will start by creating a node project. So, Create a new folder with the name ‘node-auth’ and follow the steps below. All the project files should be inside the ‘node-auth’ folder.

npm init

npm init will ask you some basic information about project. Now, you have created the node project, it’s time to install the required packages. So, go ahead and install the packages by running the below command.