HTTP basic Authentication Method for REST API
Hello People..!! In this blog we are going to see about The Authentication Methods for protecting our API routes results in avoiding access to the users who really shouldn't access them.
for example, routes that redirect to the user's profile page should access only to that respective user. Imagine being able to log into any user profile page see their private data that is unethical!!! therefore it is necessary to protect the routes
In this article, we will see various types of Authentication methods
- HTTP Basic Authentication in Nodejs
In this method, the client sends the request along with credentials encoded in base64 format in the Authorization header once the server verifies them then the requested data is sent back to the client
Module Installation Install express using the command ‘npm install express’ along with that nodemon, dotenv
the project structure is like…
the file index.js
Now in our application, will be adding a middleware mwbasicAuth where it decodes the credentials and checks, below is the sample code which acts as middleware or the gateway
here it extracts the authorization header from the request and decodes the token of base64 format and validates the credentials After the validation is done it calls the next() and sends the requested data back to the client
Hope this blog gave you a clear idea of HTTP basic Authentication
Happy learnings!!!