In this tutorial, we will learn how to use JWT in Node.js With mongoDB to secure endpoints and even authenticate users.
What is authentication and authorization
Authentication and authorization are the two words used in the security world.
Authentication
Authentication is the process of verifying a user’s identification through the acquisition of credentials and using those credentials to confirm the user’s identity. where as authorization is a way to provide permission to someone to access a particular resource. These are the two basic security terms and hence need to be understood thoroughly.
Authorization
Authorization is the process of allowing authenticated user’s access to resources by determining whether they have system access permissions. By giving or denying specific licenses to an authenticated user’s, authorization enables you to control access privileges.
What is JWT
JSON Web Token is a proposed Internet standard for creating data with optional signature Or optional encryption whose payload holds JSON that asserts some number of claims. The tokens are signed either using a private secret or a public/private keys. And JWT specifies a compact and self-contained method for communicating information as a JSON object between two parties. Because it is signed, this information can be checked and trusted.
Prerequisites
To follow along with this tutorial, you will need:
- A normal understanding of Node.js And JavaScript.
- A basic understanding of MongoDB or any database of your choice.
- A basic understanding Postman And Thunder Client
API development using JWT token for authentication in Node.js and MongoDB database
To get started, we’ll need to set up our node js project.
Step 1 - Create a directory and initialize npm
Create a directory and initialize npm by typing the following command:
mkdir node-project
cd node-project
npm init -y
We are using visual studio You can execute cmd for opening visual studio:
$ code.
Step 2 - Create files and directories
We have step 1, we initialized npm with the command npm init -y, which automatically created a package.json file.