Node.js Passport Facebook Authentication Using Express and MongoDB Full Project

You are currently viewing Node.js Passport Facebook Authentication Using Express and MongoDB Full Project

Welcome folks today in this tutorial we will be building a node.js facebook authentication system from scratch using express framework and mongodb database in which we will be storing the details of the user such as name email and profile picture. So let’s get started and all the source code is given in the blog to copy and also download. And also watch the entire youtube video which is shown below to follow with the blog.


So first of all install all these dependencies which are listed below to get started with the project.

  1. Express
  2. Passport
  3. Passport-facebook
  4. ejs
  5. nodemon



Just execute this command to install these dependencies. And after that you will get the package.json file in which you will get information about your project and all these dependencies listed out.

Now we will make the app.js file which will be the starting point of the application. In this block of code we will start the express server by requiring it.And also we will be setting the view engine ejs to it. And loading the index.ejs file when opening the home route. And lastly we will start the application at port 5000


Now make a new folder views inside your root directory of your project to store all your views

And now copy paste all the code to index.ejs file


And now in this file we have just bootstrap jumbotron in which we have a simple heading of node authentication and we have some icon of font awesome that we are using and then we have a simple anchor element which points to a route /auth/facebook something like this as shown below.


And now we just have to make a simple get route for this address /auth/facebook so when we go to it a facebook window will appear asking for the permissions.

For that we need to configure passport into our application. For that we will require client id client secret and callback url. To get these things you need to go to Facebook developer console to get these credentials.


So get your App id and App secret after creating your project in facebook developer console.

Now we will be requiring the passport and passport-facebook dependencies in app.js and making a new facebook strategy for passport passing these three things something like this



Now we will initialize the passport middleware functions like this



Now we will start a new strategy for passport for facebook passing client id client secret and callback url like this



Now we will make the route which handles the facebook authentication for us when we click the login button like below



We are passing the scopes to the function accessing profile info about user

Now we will make the callback url route to handle when we are authenticated



In this block of code we are going to profile route if we are successfully autheticated or going to failure redirect if any sort of error takes place


This is whole code . When we run this



Now we will store our information in database using a model like below. Create a new file User.js inside models folder and copy paste the following code.



Now make Changes to your app.js file and write this code






Logout request code shown below






Leave a Reply