Full-Stack Web Development with Vue.js and Node

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

The callback we have defined in the [preceding piece of code is
http://localhost:8081/login/twitter/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 a successful request, our application will get redirected to the home page.


Adding necessary routes for Twitter login


Now, let's add the necessary routes for when we click on the Login button and when we


receive the callback from Twitter. In the same file, twitter.js, add the following routes:


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

module.exports.controller = (app) => {
// twitter strategy
passport.use(new Strategy({
consumerKey: config.TWITTER_APP_ID,
consumerSecret: config.TWITTER_APP_SECRET,
callbackURL: '/login/twitter/return',
profileFields: ['id', 'displayName', 'email'],
},
(accessToken, refreshToken, profile, cb) => {
// Handle twitter login
}));

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

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