Search code examples
node.jsexpresspassport.jspassport-localconnect-flash

Intercepting flash message


I am using Passport authentication with 'local' strategy for my application. I want the user to be able to know if the username and password entered by user is invalid and if valid then redirect to dashboard. This is how i am authenticating the user:

router.post('/login', passport.authenticate('local', {failureRedirect: '/login', failureFlash: 'Invalid username or password.'}), function(req, res, next){
    res.redirect('/users/dashboard');
});

Redirect part works fine, but the user enters invalid user/pass the flash message in failureFlash: 'Invalid username or password.' dosent show up on the login page. This is how i handle my login route:

router.get('/login', function(req, res, next) {
  res.render('pages/login', {'title' : 'VoteCenter - Login', message: req.flash('message')});
});

What do i need to use in req.flash('??????') to intercept the flash mesaage coming from failureFlash?


Solution

  • You can get message through req.flash('error')(default)

    also you can pass failureFlash object with your custom message name like it:

    failureFlash: { type: 'authError', message: 'Invalid username or password.'}
    

    then get message req.flash('authError')