Basic Authentication using Expressjs in Nodejs API with Base64 encoding

Basic Authentication using Expressjs and Nodejs Base64 encoding

Express.js gives us an out-of-box solution for ding basic authentication via its request parameter.

We all know that the callback function has request and response (req,res). So, when the user makes a request to our Node.js, we receive Header information as well in the request object. In that if 'authorization' property is set and given a value, that can be used. We can ask our REST API consumers to send username and password in Base64 encoding as the value of this authorization property.

The base64 coded value can be obtained using https://www.base64encode.org/
For us, the validation is if username is equal to password we give the data to the consumer of the API. In real life scenario, we might do it against the data values in our DB.

Let us take a look at the code. The code itself has explanation. Please drop a comment /share this post if you like it or even if you have any queries. See you soon with one more tutorial.

2 comments: Leave Your Comments