UNPKG

1.35 kBJavaScriptView Raw
1"use strict";
2
3
4const Auth = require('./Auth');
5const Strategy = require('passport-google-oauth2')
6 .Strategy;
7
8/**
9 * OAuth login using google login provider
10 *
11 * Requires ```passport-google-oauth2``` package.
12 */
13class GoogleAuth extends Auth
14{
15
16 /**
17 * @param {object} options see Auth class + additional options for email configuration.
18 */
19 constructor(options = {})
20 {
21 super('google', options);
22 this.description.redirect = true;
23
24 /**
25 * OAuth 2 Client ID
26 */
27 this.googleClientID = options.googleClientID;
28
29 /**
30 * OAuth 2 Client Secret
31 */
32 this.googleClientSecret = options.googleClientSecret;
33 }
34
35 /**
36 * @override
37 */
38 install(app, prefix, passport)
39 {
40 passport.use(new Strategy({
41 clientID: this.googleClientID,
42 clientSecret: this.googleClientSecret,
43 callbackURL: `${prefix}/callback.json`,
44 scope: ['email', 'profile'],
45 state: true,
46 passReqToCallback: true,
47 proxy: true,
48 }, (req, accessToken, refreshToken, profile, done) =>
49 this.handleUserLoginByProfile(null, profile, done, req)));
50
51 app.all(`${prefix}/login.json`, passport.authenticate('google', {}));
52
53 app.all(`${prefix}/callback.json`, passport.authenticate('google', this.authenticateOptions), this.loggedIn(true));
54 }
55
56}
57
58module.exports = GoogleAuth;