Nodejs REST API Authentication using Passport

Nodejs REST API Authentication using Passport.js 

Hi All, I know its been a while that I have written an article here. However, I have not waster a single day. I was into writing concepts on JavaScript Module pattern and JavaScript Prototype Pattern. 

I will soon put up all the content explaining JavaScript Prototype pattern and Prototype by default in great detail here.
Coming back to the current article, Nodejs , the serverside JavaScript framework is famous for writing REST APIs. It is also famous for writing webapps (web pages) using ejs/ jade/hapi etc as the web frameworks.

In this article, I will help you out writing an API in node and authenticating it using passportjs.org's passport-http basicStrategy method. In real world scenarios, it is advised to use SSL to implement authentication. 

So, lets jump into the code an build an API.

1. do the below three npm first in your project.

  • npm express 
  • npm passport
  • npm passport-http.

2. Now, define the require statements.

3. define a port where the API is exposed for use and the app listens to.

4. Let the app know that it has to use passport.
5. Initialize the passport
6. Define the BasicStrategy via the passport-http.
7. Let passport know that it has to use BasicStrategy to do the authentication
8. As per the documentation, BasicStrategy  takes three arguments. They are the input username, password and a callback named 'done'.
9. 'done' lets the passport know if there was a success or a failure during the authentication.
10. Based on the result of the authentication, that is injected to the express framework via app that uses this passport to allow the user to the next level of query using the next() call of the express.
11. For http, because the APIs are session less, the session is false in the code.
12. In our example, we return true when username and password are same. However, in real world example, the username and password are to be cross verified against your database user table details.
13. In our example, once the user is authenticated, we display some static data. In real world, it is some data from database.
Complete code is below. You can also look at the complete code here.

So there you go, below are the screenshots when i run the application. Hope this helps. I will soon upload the application to github. Thank you so much. See you soon with more JavaScript, C# and Node.js code samples and code snippets.

4 comments: Leave Your Comments

  1. Node.js frameworks a JavaScript runtime. It is actually behaves as a wrapper around the V8 JavaScript runtime. Many people use the JavaScript programming languages extensively for programming the interfaces of Web sites.
    web development company Perth

  2. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles.

    Digital Marketing Training in Chennai

    Digital Marketing Course in Chennai