The original project demonstrates how to use Express 4.x and Passport to log users in with Facebook.
As an addition to the original project, todos-express-facebook has been modified to use Sequelize and passport-facebook.
Use this example as a starting point for your own web applications, if you're trying to use: NodeJS, Express, Passport, MySQL, and Sequelize.
To get started with this example, clone the repository and install the dependencies.
$ git clone https://github.com/tr-ace/sequelize-express-passport-template.git
$ cd sequelize-express-passport-template
$ 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/www.facebook.com
Once credentials have been obtained, create a .env
file and add the following
environment variables:
FACEBOOK_CLIENT_ID={{INSERT_APP_ID_HERE}}
FACEBOOK_CLIENT_SECRET={{INSERT_APP_SECRET_HERE}}
DB_HOST={{INSERT_DB_IP_ADDRESS_HERE}}
DB_NAME={{INSERT_DB_NAME_HERE}}
DB_USER={{INSERT_DB_USERNAME_HERE}}
DB_PASSWORD={{INSERT_DB_PASSWORD_HERE (IF ONE EXISTS)}}
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 Sequelize and MySQL for storing user accounts and the to-do data. MySQL is a free and commonly used database for development and production environments.
Added to the scaffolding are files which add authentication to the application.
-
This file initializes the database connection.
-
This file initializes Passport. It configures the Facebook strategy and supplies the serialization functions used for session management.
-
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/www.facebook.com
This route begins the authentication sequence by redirecting the user to Facebook.
-
POST /oauth2/redirect/www.facebook.com
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.
-