Code coverage report for server/app/configure/authentication/google.js

Statements: 0% (0 / 18)      Branches: 0% (0 / 2)      Functions: 0% (0 / 6)      Lines: 0% (0 / 18)      Ignored: none     

All files » server/app/configure/authentication/ » 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 57 58 59 60                                                                                                                       
'use strict';
 
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
 
module.exports = function (app, db) {
 
    var User = db.model('user');
 
    var googleConfig = app.getValue('env').GOOGLE;
 
    var googleCredentials = {
        clientID: googleConfig.clientID,
        clientSecret: googleConfig.clientSecret,
        callbackURL: googleConfig.callbackURL
    };
 
    var verifyCallback = function (accessToken, refreshToken, profile, done) {
 
        User.findOne({
                where: {
                    google_id: profile.id
                }
            })
            .then(function (user) {
                if (user) {
                    return user;
                } else {
                    return User.create({
                        google_id: profile.id
                    });
                }
            })
            .then(function (userToLogin) {
                done(null, userToLogin);
            })
            .catch(function (err) {
                console.error('Error creating user from Google authentication', err);
                done(err);
            });
 
    };
 
    passport.use(new GoogleStrategy(googleCredentials, verifyCallback));
 
    app.get('/auth/google', passport.authenticate('google', {
        scope: [
            'https://www.googleapis.com/auth/userinfo.profile',
            'https://www.googleapis.com/auth/userinfo.email'
        ]
    }));
 
    app.get('/auth/google/callback',
        passport.authenticate('google', {failureRedirect: '/login'}),
        function (req, res) {
            res.redirect('/');
        });
 
};