Implementing Facebook Login using Nodejs and Express

In this article, we will see how to implement facebook login in application using Nodejs and Express. Implementing Facebook Login using Nodejs and Express.

Facebook Login is widely used for authentication in lots of applications. Instead of asking user to register, application can simply using social websites to get the user information

Recent Article in Node.js,

Crafting multi-stage builds with Docker in Node.js

Building a Production – Ready Node.js App with TypeScript and Docker

Setting up Developer's Account

we need Facebook developer credentials for application development. So , go to, and create an application.

app creation

As a result, it creates an application an redirects to dashboard. After that, go to Settings -> Basic to get the app credentials.

Capture 1

Now, you have the application credentials. it's time to write some code.


passportjs is used for facebook authentication in Nodejs/Express application.

That is to say, Install the dependencies for application, such as

  • express - Node.js Framework to run the web server.
  • Passportjs - it is an authentication middleware for Node.js application.
  • handlebars - Templating engine for Javascript application.
1npm install --save express express-handlebars passport passport-facebook

Add the following code to setup the express server.

1const express = require("express")
3const app = express()
5app.get("/", (req, res) => {
6 res.send("Server")
9app.listen(3000, () => {
10 console.log("Server is Running in Port 3000")

You can run the application using the script node app.js.

After that, visit the url http://localhost:3000 to see the output.

Now, it is time to set up web page for button and data list after login. For that, we need handlebars. add the following code for handlebars initialization.

1const express = require('express');
2const exphbs = require('express-handlebars');
4const app = express();.
6app.engine('handlebars', exphbs());
7app.set('view engine', 'handlebars');
9app.get("/",(req,res) => {
10 res.render('home');
13app.listen(3000,() => {
14 console.log("Server is Running in Port 3000");

it imports the express handlebars library and set it as the template engine for the express application.

After that, Create a folder called views in the root directory. Inside that, create a folder layout that contains the basic layout.

Meanwhile , If you are new to Handlebars. i recommend you to watch this tutotial


Views contains all the handlebar files. Firstly, add the following code in the main.handlerbars. it will create a basic html page for application. it can be reused in all the subsequent handlebar files.

1<!DOCTYPE html>
3 <head>
4 <meta charset="utf-8" />
5 <title>Example App</title>
6 <link
7 rel="stylesheet"
8 href=""
9 />
10 </head>
12 <body>
13 {{{body}}}
14 </body>

After that, add the following code in home.handlebars, it creates an button element through which user can login.

1<div class="ui one column stackable center aligned page grid">
2 <div class="column twelve wide">
3 <div class="ui facebook button">
4 <i class="facebook icon"></i>
5 <a href="/auth/facebook" style="color: white;">Login with Facebook</a>
6 </div>
7 </div>

Likewise , data.handlebars contains the user data after facebook login.

1<div class="ui two column centered grid">
2 <div class="column">
4 <h2>Display Name : {{user.displayName}}</h2>
5 </div>

Passportjs Implementation

Now, it's time to integrate passport.js with express application.

Import the passport and passport-facebook package in the app.js

1const passport = require("passport")
2const FacebookStrategy = require("passport-facebook").Strategy

secondly , initialize passport and passport sesstion.


passport.session() acts as a middleware to alter the req object and change the user value in the request session

Moreover , To store and retrieve in the session. the user value should be serialized.

1passport.serializeUser(function(user, done) {
2 done(null, user)
5passport.deserializeUser(function(user, done) {
6 done(null, user)

After that, we use the Passport Facebook Strategy to authenticate the application.

2 new FacebookStrategy(
3 {
4 clientID: "Client ID",
5 clientSecret: "Client Secret",
6 callbackURL: "http://localhost:3000/auth/facebook/callback",
7 },
8 function(accessToken, refreshToken, profile, cb) {
9 return cb(null, profile)
10 }
11 )

That is to say, Passport registers the Facebook Strategy for authentication. Mainly it takes ClientID and ClientSecret to register strategy.

As a result, we can use it as a middleware in express app routes.

Above all , when authentication is done, it will call the callback function with the user profile data.

As a result Once login is successfull, it calles the callback url with the user profile data. it sends the data to handlebars to display it in the dashboard.

1app.get("/auth/facebook", passport.authenticate("facebook"))

Complete Code

1const express = require("express")
3const exphbs = require("express-handlebars")
4const passport = require("passport")
5const FacebookStrategy = require("passport-facebook").Strategy
6let app = express()
8app.engine("handlebars", exphbs())
9app.set("view engine", "handlebars")
14passport.serializeUser(function(user, done) {
15 done(null, user)
18passport.deserializeUser(function(user, done) {
19 done(null, user)
23 new FacebookStrategy(
24 {
25 clientID: "Client ID",
26 clientSecret: "Client Secret",
27 callbackURL: "http://localhost:3000/auth/facebook/callback",
28 },
29 function(accessToken, refreshToken, profile, cb) {
30 return cb(null, profile)
31 }
32 )
35app.get("/auth/facebook", passport.authenticate("facebook"))
38 "/auth/facebook/callback",
39 passport.authenticate("facebook", { failureRedirect: "/" }),
40 function(req, res) {
41 console.log("req", req.user)
42 res.render("data", {
43 user: req.user,
44 })
45 }
48app.get("/", (req, res) => {
49 res.render("home", {
50 user: req.user,
51 })
54app.listen(3000, () => {
55 console.log("Server is Running in Port 3000")


To sum up , passport is a simplest way to implement social login in express application.

In conclusion, we learned how to implement facebook login in express application.

To Read More

Kubernetes for Nodejs developers

Do you keep hearing the word kubernetes in the tech community and you couldn't u...

TypeScript Interfaces vs Types

In this article, we will see what are interfaces and types and the difference be...

How to find project ideas to practi...

Ever wondered what how to get a real world experience on web development while w...

Building a Production-grade Nodejs,...

This article is the first part of building a production grade nodejs,graphql and...

Modern React Redux Tutorials with R...

This tutorial explain how you can build an application using modern react redux ...

Building a Piano with React Hooks

In this article, we will see how to build a piano with react hooks. Building a P...

TypeScript Basics - The Definitive ...

In this article, we will learn some basics of typescript which helps you to deve...

Here's why podman is more secured t...

In this article we will see about podman and why it is more secured way to run c...

What is gRPC ? How to implement gRP...

Everyone talks about gRPC. Have you ever wonder how it works or how to implement...