Node JS

Node JS User Authentication System 2022

Hello and welcome in this post I am going to discuss about Node JS User Authentication System. Recently I could read a user authentication system in Node JS and I think that I should share it with you that’s why I am posting it here on my blog. In this post I will be going to give complete tutorial of this user authentication system and at last I will also give you link to the GitHub Repo. So let’s get started.

Node JS User Authentication System

Install NPM Packages

First of all we are going to install several npm packages that we are going to use in this project. These packages are given below.

  1. express
  2. bcryptjs
  3. dotenv
  4. body-parser
  5. mongodb
  6. mongoose

Create app.js

app.js will be the main file of our project which will contain quotes related to server and database connection.

The above code will connect to MongoDB DB using mongoose and start a http server using Express JS.

user.model.js

Now we are going to create a mongoose model to structure data that we are going to pass. We are using mongoose because it makes very easy to make models and handle what type of data we are going to pass.

The above code is defining type of data that we are going to pass through our user model. At last we are exporting user model and user schema so that we can use them in route.

user.route.js

In this file we are going to define and write code of all our routes. This project will contain 5 routes including get all users, get specific user, register an user, login user and delete an user.

Get All Users

The above route is querying the database and giving back list of all the users excluding their password. We are also using an if statement to check if users list is empty or not.

Get Specific User

This route is getting information of a specific user based upon his user ID that will be passed as a parameter at the end of URL.

Create New User

This route is simply creating a new object of user model and getting data from body which will be sent through frontend and saving this data into database using save() function. Take a look at password-hash. In password hash we are using bcryptjs library to Hash the password that we are going to get from frontend as a parameter of password.

The syntax for bcryptjs is => bcryptjs.hashSync(“Text to hash”, salt)

Login User

This is the most important route of our project. In this route we are comparing email and password which the user is giving us with the email and password that is present in our database.

First of all we are checking whether the email is present in our database or not that the user is trying to login with.

If the email is present then we are using another if statement in which we are comparing the password that the user is sending with the password hash that is present in our database using compareSync function.

If both of the conditions are true you we are giving back a login successful message and if conditions are poles we are giving back and invalid password message.

Delete User

This is a very simple route in this route we are just using findOneAndRemove function that is present in mangoose to delete a specific user by getting his or her email as a parameter.

Also Read: – Best Android Development Course 2022 Free + Paid

GitHub Link: – Here

Conclusion

I hope that this post was helpful for you and you found it informative. Thank you for visiting our blog.

adminSUSHANT

Student | Programmer | Blogger

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please Disable Ad-Blocker