Full-Stack Web Development with Vue.js and Node

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

Adding configurations for Google app


Let's configure the Google Strategy just as we did for the Facebook and Twitter strategies.


We will create a separate file to handle Google login so that the code is simple. Let's create a
file called google.js inside the controllers folder and add the following contents to it:


const User = require('../models/User');
const passport = require('passport');
const config = require('./../config/Config');
const Strategy = require('passport-google-oauth20').OAuth2Strategy;

module.exports.controller = (app) => {
// google strategy
passport.use(new Strategy({
clientID: config.GOOGLE_APP_ID,
clientSecret: config.GOOGLE_APP_SECRET,
callbackURL: '/login/google/return',
},
(accessToken, refreshToken, profile, cb) => {
// Handle google login
}));
};

As we did in the Facebook and Twitter strategies, the first line imports the Google Strategy.
The configuration takes the following three parameters: clientID, clientSecret, and


callback URL. The clientID and clientSecret are the App ID and App Secret of the


Google application we just created.


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_client_id>,
TWITTER_APP_SECRET: <twitter_client_secret>,
GOOGLE_APP_ID: <google_client_id>,
GOOGLE_APP_SECRET: <google_client_secret>
}

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

Free download pdf