Full-Stack Web Development with Vue.js and Node

(singke) #1
Building OAuth Strategies with passport.js Chapter 7

// Handle facebook login
}));
};

In the preceding code, the first line inside the exports method imports the Facebook


Strategy. The configuration takes three parameters: clientID, clientSecret, and


callback URL. clientID and clientSecret are the App ID and App Secret for your


Facebook app, respectively.


Let's add those secrets into our config file. In config/Config.js, let's add our Facebook


keys, the facebook_client_id and facebook_client_secret:


module.exports = {
DB: 'mongodb://localhost/movie_rating_app',
SECRET: 'movieratingappsecretkey',
FACEBOOK_APP_ID: <facebook_client_id>,
FACEBOOK_APP_SECRET: <facebook_client_secret>
}

The callback URL is the URL that you want to route your application to after the successful


transaction with Facebook.


The callback we have defined here is


http://127.0.0.1:8081/login/facebook/return, which we have to define. The


configuration is followed by a function that takes the following four parameters:


accessToken
refreshToken
profile
cb (callback)

Upon successful request, our application will get redirected to the home page.


Adding necessary routes for Facebook login


Now, let's go ahead and add the necessary routes for when we click on the login button and
when we receive the callback from Facebook. In the same file, facebook.js, add the


following routes:


const User = require("../models/User");
const passport = require('passport');
const config = require('./../config/Config');
Free download pdf