Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?


Failed to load latest commit information.
Latest commit message
Commit time
August 26, 2022 10:35
August 26, 2022 10:31
August 26, 2022 12:16
August 26, 2022 11:52
August 26, 2022 12:16
August 26, 2022 10:35
August 11, 2015 11:58
June 22, 2021 16:20
August 26, 2022 12:00
August 26, 2022 12:12

This example demonstrates how to use Express 4.x and Passport to log users in with Facebook. Use this example as a starting point for your own web applications.

Quick Start

To get started with this example, clone the repository and install the dependencies.

$ git clone
$ cd express-4.x-facebook-example
$ npm install

This example requires credentials from Facebook, which can be obtained by creating an app in the App Dashboard. The OAuth redirect URI of the app should be set to: http://localhost:3000/oauth2/redirect/

Once credentials have been obtained, create a .env file and add the following environment variables:


Start the server.

$ npm start

Navigate to http://localhost:3000.


This example illustrates how to use Passport and the passport-facebook strategy within an Express application to log users in with Facebook.

The example builds upon the scaffolding created by Express generator, and uses EJS as a view engine and plain CSS for styling. This scaffolding was generated by executing:

$ express --view ejs express-4.x-facebook-example

The example uses SQLite for storing user accounts. SQLite is a lightweight database that works well for development, including this example.

Added to the scaffolding are files which add authentication to the application.

  • boot/db.js

    This file initializes the database by creating the tables used to store user accounts and credentials.

  • boot/auth.js

    This file initializes Passport. It configures the Facebook strategy and supplies the serialization functions used for session management.

  • routes/auth.js

    This file defines the routes used for authentication. In particular, there are three routes used to authenticate with Facebook:

    • GET /login

      This route renders a page that prompts the user to login with Facebook.

    • GET /login/federated/

      This route begins the authentication sequence by redirecting the user to Facebook.

    • POST /oauth2/redirect/

      This route completes the authentication sequence when Facebook redirects the user back to the application. When a new user logs in, a user account is automatically created and their Facebook account is linked. When an existing user returns, they are logged in to their linked account.


The Unlicense