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

Statements: 73.91% (17 / 23)      Branches: 60% (6 / 10)      Functions: 100% (6 / 6)      Lines: 80.95% (17 / 21)      Ignored: none     

All files » server/app/configure/authentication/ » local.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  1 1   1   2       2 1             1       1           2     2   1   1   1             1 1   1             1          
'use strict';
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
 
module.exports = function (app, db) {
 
    var User = db.model('user');
 
    // When passport.authenticate('local') is used, this function will receive
    // the email and password to run the actual authentication logic.
    var strategyFn = function (email, password, done) {
        User.findOne({
                where: {
                    email: email
                }
            })
            .then(function (user) {
                // user.correctPassword is a method from the User schema.
                Iif (!user || !user.correctPassword(password)) {
                    done(null, false);
                } else {
                    // Properly authenticated.
                    done(null, user);
                }
            })
            .catch(done);
    };
 
    passport.use(new LocalStrategy({usernameField: 'email', passwordField: 'password'}, strategyFn));
 
    // A POST /login route is created to handle login.
    app.post('/login', function (req, res, next) {
 
        var authCb = function (err, user) {
 
            Iif (err) return next(err);
 
            Iif (!user) {
                var error = new Error('Invalid login credentials.');
                error.status = 401;
                return next(error);
            }
 
            // req.logIn will establish our session.
            req.logIn(user, function (loginErr) {
                Iif (loginErr) return next(loginErr);
                // We respond with a response object that has user with _id and email.
                res.status(200).send({
                    user: user.sanitize()
                });
            });
 
        };
 
        passport.authenticate('local', authCb)(req, res, next);
 
    });
 
};