Full-Stack Web Development with Vue.js and Node

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

Adding necessary routes for LinkedIn login


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


receive the callback from LinkedIn:


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

module.exports.controller = (app) => {
// linkedin strategy
passport.use(new Strategy({
consumerKey: config.LINKEDIN_APP_ID,
consumerSecret: config.LINKEDIN_APP_SECRET,
callbackURL: '/login/linkedin/return',
profileFields: ['id', 'first-name', 'last-name', 'email-address']
},
(accessToken, refreshToken, profile, cb) => {
// Handle linkedin login
}));

app.get('/login/linkedin',
passport.authenticate('linkedin'));

app.get('/login/linkedin/return',
passport.authenticate('linkedin', { 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://localhost:8081/login/linkedin, 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 linkedin.js with the following:


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