Full-Stack Web Development with Vue.js and Node

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

module.exports.controller = (app) => {
// facebook strategy
const Strategy = require('passport-facebook').Strategy;

passport.use(new Strategy({
clientID: config.FACEBOOK_APP_ID,
clientSecret: config.FACEBOOK_APP_SECRET,
callbackURL: '/api/login/facebook/return',
profileFields: ['id', 'displayName', 'email']
},
function(accessToken, refreshToken, profile, cb) {
}));

app.get('/login/facebook',
passport.authenticate('facebook', { scope: ['email'] }));

app.get('/login/facebook/return',
passport.authenticate('facebook', { failureRedirect: '/login' }),
(req, res) => {
res.redirect('/');
});
}

In the preceding code, we have added two routes. If you remember, in Login.vue, we


have added a link to http://127.0.0.1:8081/login/facebook, which will be served


by the first route that we defined here.


Also, if you recall, in the configuration setting, we have added a callback function that will
be served by the second route, which we have defined here as well.


Now, the final thing to do is to actually log in the user using the strategy. Replace the
contents of facebook.js with the following:


const User = require('../models/User.js');
const passport = require('passport');
const config = require('./../config/Config');
const Strategy = require('passport-facebook').Strategy;

module.exports.controller = (app) => {
// facebook strategy
passport.use(new Strategy({
clientID: config.FACEBOOK_APP_ID,
clientSecret: config.FACEBOOK_APP_SECRET,
callbackURL: '/login/facebook/return',
profileFields: ['id', 'displayName', 'email'],
},
(accessToken, refreshToken, profile, cb) => {
Free download pdf