Building authentication in Node.JS

pouya javadi
2 min readMay 16, 2021

Learning about building a full authentication is definitely a fun ride. It covers many edge cases that validates every aspect of a creating a secure access to the system.

App objective:

This is a simple eCommerce application with a full functional authentication system. It is being built using JavaScript and NodeJS. I have decided to build the authentication so the user can create a profile and the app has an identity to interact with other functions such as adding products, buying, deleting, or updating the products.

At the most abstract level, Index.js is the file that runs the entire program when the app starts loading:


Cookie-session is the npm library that is being used to create session and user authentication. I also added authentication route so that the links to access signup and login pages can be easily available.

Routes > auth.js:

In this file, we have our authentication routes for signing up a new user or logging in an existing user.

I have also imported validator functions used as a checkpoint to inspect whether the user’s profile has been created, already exists, follow our username/password guidelines, or if the user is already logged in, and errors that initiate if any of the validation points fails.

Here is the snapshot of the validator functions:

Express validator is a middleware that is used to evaluate and validate username, password, and generate errors if they arise.

We also have views that generate HTML templates and helper method that shows errors:



The end result, will look something like this:

If you signup with pre-existing record, after clicking a submit button it will generate error message that the email is in use, or if the password and password confirmation does not match, it will say so.

This is a work in progress app, and I will update this blog to reflect the latest features.

Link to repo



pouya javadi

Building solutions, solving problems, engaging with others are pillars of learning in life.