Full-Stack Web Development with Vue.js and Node

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

Adding configurations for Twitter App


Now, the next step is to add the necessary routes for the Twitter login. For this, we will


need to configure the settings and callback URL. Just like we did for the Facebook Strategy,
let's create a separate file to set up our Twitter login. Let's create a new file inside the


controllers directory called twitter.js and add the following contents:


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
}));
};

As we did in the Facebook Strategy, the first line imports the Twitter Strategy. The


configuration takes the following three parameters: clientID, clientSecret, and a


callback URL. The consumerKey and consumerSecret are the App ID and App Secret


for your Twitter application app, respectively.


Let's add those secrets into our config file. In config/Config.js, add 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>,
TWITTER_APP_ID: <twitter_consumer_id>,
TWITTER_APP_SECRET: <twitter_consumer_secret>
}

The callback URL is the URL that you want to route your application to after the successful
transaction with Twitter.

Free download pdf