NodeJS MongoDB Security and Authentication

In the previous tutorial, we have successfully created a simple CRUD using NodeJS API. We will go further based on the previous code that we have made to create the security and authorization method using JSON Web Token(JWT) and Hashing.

Setting Up the User Model

Installing NPM Validator Module

We will modify the User model in order to be able to use validation. First, we need to install the validator module :

Modify models/user.js

Modify the user.js by adding some attributes to the email and adding password and token credentials:

Creating POST /users Route

Next we’ll create a route to insert user email and password to the database. Modify server.js and add the following code :

server.js

Turn on your Robo 3T database and try the url using Postman :

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.