-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (39 loc) · 1.49 KB
/
index.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
let passport = require('passport')
let GoogleStrategy = require('passport-google-oauth').OAuth2Strategy
module.exports = app => {
app.on('startup loaded', () => {
if(!app.auth || !app.auth.google)
throw new Error('Missing Google configuration. Please define app.auth.google')
if(!app.api.google || !app.api.google.id || !app.api.google.secret)
throw new Error('Missing Google API keys. Please add them to security/api-keys.json')
if(!app.auth.google.login)
throw new Error("app.auth.google.login needs to be defined")
if(app.auth.google.login.constructor.name === 'GeneratorFunction')
app.auth.google.login = Promise.coroutine(app.auth.google.login)
let config = {
callbackURL: app.production ? `https://${app.config.domain}/auth/google/callback` : '/auth/google/callback',
passReqToCallback: true,
clientID: app.api.google.id,
clientSecret: app.api.google.secret
}
// Register Google strategy
passport.use(new GoogleStrategy(config,
function(request, accessToken, refreshToken, profile, done) {
app.auth.google.login(profile._json)
.then(user => done(undefined, user))
.catch(error => done(error, false))
}
))
// Google login
app.get('/auth/google', passport.authenticate('google', {
scope: app.auth.google.scopes || [
'https://www.googleapis.com/auth/plus.login',
'email'
]
}))
// Google callback
app.get('/auth/google/callback',
passport.authenticate('google', app.auth.google.onLogin || { successRedirect: '/' })
)
})
}