Erik
Erik

Reputation: 14750

Passport local strategy not getting called when email or password empty

I use the following passportjs LocalStrategy:

passport.use(new LocalStrategy({
  usernameField: 'email',
  passwordField: 'password'
},
  function(username, password, done) {
  // ...
  }
));

All works fine if I do provide email and password properties in POST request. But if one of then is empty then this middleware don't get executed but redirect happens.

I need to know what happens and pass req.flash to the client to handle this exception. How could I do it?

Upvotes: 2

Views: 1492

Answers (1)

r0-
r0-

Reputation: 2498

You could use express-validator. It can check your email/password fields in general that they are not empty.

Just require and use it in your app.js entry point:

var expressValidator = require('express-validator');
app.use(bodyParser());
app.use(expressValidator()); // then let the app use it, place it HERE after the bodyParser()

and in your login/signup route:

router.post('/signup', function (req, res) {
     req.assert('email', 'Email is not valid!').isEmail();
     req.assert('password', 'Password is empty!').notEmpty();

      var err = req.validationErrors();

      if (err) {
        req.flash('errors', errors);
        return res.redirect('/login');
      }
// Your passport here....
}

And then do your passport stuff like login or signup.

Upvotes: 3

Related Questions