I want to show error messages for users in cases of wrong credentials. How could I do that in Express JS? I tried the following code but it doesn't work:
app.get('/', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err) }
if (!user) { return res.send($( "<div class='ui-loader ui-overlay-shadow ui-body-e ui-corner-all'><h1>YOUR MESSAGE</h1></div>" )
.css({ "display": "block", "opacity": 0.96, "top": $(window).scrollTop() + 100 })
.appendTo( $.mobile.pageContainer )
.delay( 800 )
.fadeOut( 400, function() {
$( this ).remove();
})
); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/account');
});
})(req, res, next);
});
What is the problem with this?
Thanks,
Is the error message coming from the strategy's verify callback? Like so:
// verify callback
return done(null, false, { message: 'Invalid password' });
If that's the case, it'll wind up as the info
argument to the snippet above:
passport.authenticate('local', function(err, user, info) {
// arguments to this callback are what was given to done()
// info.message => 'Invalid password'
var msg = "<h1>" + info.message + "<h1>"
res.send(msg)
}