0

I am logging in a user with passport local:

// route to log in 
    app.post('/login', passport.authenticate('local-login'), function(req, res) { 
        res.send(req.user); 
    }); 

This will only work in the case of success, i.e. when req.user is defined. How do I check for an error in the request? Essentially I want to take the error that was pulled from my passport authentication code in the done() call and send that back to the client to display the proper error message:

passport.use('local-login', new LocalStrategy({
        username : 'email',
        password : 'password',
        passReqToCallback : true 
    },
    function(req, email, password, done) { 

        console.log('logging in user: ' + email);
        // find a user whose email is the same as the forms email
        // we are checking to see if the user trying to login already exists
        Landlord.findOne({ 'local.email' :  email }, function(err, user) {
            if (err) return done(err);
            if (!user) return done(null, false, { message: 'Incorrect username.' });
            if (!user.validPassword(password)) {
                console.log('wrong pw');
                return done(null, false, { message: 'Incorrect password.' });
            }
            return done(null, user);
        });
    }));
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
thebiglebowski11
  • 1,451
  • 10
  • 41
  • 76

0 Answers0