-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgoogle.js
56 lines (50 loc) · 1.53 KB
/
google.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const passport = require('passport'),
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
{
verify,
getAuthUrl,
getPathOrBase,
getCallbackUrl,
generateStrategyName,
} = require('../utils');
/**
* Google authentication strategy
*
* @param {object} site
*/
function createGoogleStrategy(site) {
passport.use(`google-${site.slug}`, new GoogleStrategy({
clientID: process.env.GOOGLE_CONSUMER_KEY,
clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
callbackURL: getCallbackUrl(site, 'google'),
userProfileURL: process.env.GOOGLE_PROFILE_URL,
passReqToCallback: true
},
verify({
username: 'emails[0].value',
imageUrl: 'photos[0].value',
name: 'displayName',
provider: 'google'
})));
}
/**
* add authorization routes to the router
* @param {express.Router} router
* @param {object} site
* @param {object} provider
*/
function addAuthRoutes(router, site, provider) {
const strategy = generateStrategyName(provider, site);
router.get(`/_auth/${provider}`, passport.authenticate(strategy, { scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'
]}));
router.get(`/_auth/${provider}/callback`, passport.authenticate(strategy, {
failureRedirect: `${getAuthUrl(site)}/login`,
failureFlash: true,
successReturnToOrRedirect: getPathOrBase(site)
})); // redirect to previous page or site root
}
module.exports = createGoogleStrategy;
module.exports.addAuthRoutes = addAuthRoutes;