Im trying to flash error messages on failed login (ExpressJS 4.2 and PassportJS)
My code is:
//Admin Route =====
router.get('/admin', function(req, res) {
res.render('admin', {message: '', user: ''});
});
router.post('/admin', passport.authenticate('loginAdmin',{ successRedirect: '/panel',
failureRedirect: '/admin',
failureFlash: true })
);
//Passport local strategy =====
passport.use('loginAdmin', new LocalStrategy(
function(username, password, done) {
modeloUsuario.findOne({ nombre: username, password: password }, function(err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, {message: 'Incorrect username.'}); //Error to show
}
return done(null, user);
});
}
));
But "message" in get request of /admin never gets replaced with "Incorrect username." Am I doint it wrong? Thanks in advance!
It looks like you're redirecting back to /admin, and in the /admin route handler you're setting message to an empty string. Your admin route handler fires after your passport strategy, so you're clearing the message out there.