title: MEAN stack auth with passport: login not working (but I can create new user)
body: <p>So I am trying to add local authentication to my MEAN stack app.
  Everything was running smoothly. Sign up works great. But I cannot log in as a user.</p>
  
  <p>I am getting:
  POST /login 302 3.979 ms - 58
  on the console (via Morgan)
  But there is certainly an error (express routes to the "failureRedirect" set via passport)</p>
  
  <p>Could it be a problem reading from the db? Why would I be able to write to it then...</p>
  
  <p>A test with chai reveals that the program cannot "find user by email" from the db via User Model</p>
  
  <p>anyone have any clue why this is happening?</p>
  
  <p>Here is my passport strategy code: </p>
  
  <pre><code>/config/passport.js
  var LocalStrategy   = require('passport-local').Strategy;
  
  var User            = require('../models/users');
  
  
  module.exports = function(passport) {
  
  
  // passport session setup ==================================================
  
  passport.serializeUser(function(user, done) {
      done(null, user.id);
  });
  
  
  passport.deserializeUser(function(id, done) {
      User.findById(id, function(err, user) {
          done(err, user);
      });
  });
  
  
  // local login =============================================================
  
  
  
  
  passport.use('local-login', new LocalStrategy({
  
      usernameField : 'email',
      passwordField : 'password',
      passReqToCallback : true 
  },
  function(req, email, password, done) { 
  
      User.findOne({ 'local.email' :  email }, function(err, user) {
  
          if (err)
              return done(err);
  
          if (!user)
              return done(null, false, req.flash('loginMessage', 'No user found.')); 
  
  
          if (!user.validPassword(password))
              return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); 
  
          return done(null, user);
      });
  
  }));
  
  
  
  
  // local sign up =============================================================
  
  
  passport.use('local-signup', new LocalStrategy({
  
      usernameField : 'email',
      passwordField : 'password',
      passReqToCallback : true 
  },
  function(req, email, password, done) {
  
      User.findOne({ 'local.email' :  email }, function(err, user) {
  
          if (err)
              return done(err);
  
  
          if (user) {
              return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
          } else {
  
  
              var newUser            = new User();
  
  
              newUser.local.email    = email;
              newUser.local.password = newUser.generateHash(password); // use the generateHash function in our user model
  
              newUser.save(function(err) {
                  if (err)
                      throw err;
                  return done(null, newUser);
              });
          }
  
      });
  
  }));
  
  };
  </code></pre>
  
  <p>Here's the routes:</p>
  
  <pre><code>// /login ======================================================================
  
  app.route('/login')
  .get(function(req, res) {
    res.setHeader('Content-Type', 'text/html');
    res.render('login.html');
  })
   .post(passport.authenticate('local-login', {
          successRedirect : '/profile', 
          failureRedirect : '/'
  
      })); 
  
  
  // /signup ======================================================================
  
  app.route('/signup')
  .get(function(req, res) {
    res.setHeader('Content-Type', 'text/html');
    res.render('signup.html');
  })
  .post(passport.authenticate('local-signup', {
          successRedirect : '/profile', 
          failureRedirect : '/'
         }));
  </code></pre>