Hi guys, my ignorance is not letting me display login error messages on passport.js . I’d like to pass to my ejs login page the message
defined on passport.use
, but I get lost in the flow, for I’m not sure who is handling the done function, but I understand that somehow it is connected to the passport.authenticate
.
It seems that failureRedirect
is calling res.render under the hood, and it would be nice to be able to pass to it the message from the passport.use
I tried unsuccessfully to set up up the Flash Messages, but I’d like to avoid that if it is possible. Here’s part of the code, it is mainly boilerplate
passport.use(
new LocalStrategy((username, password, done) => {
User.findOne({ username: username }, (err, user) => {
if (err) {
return done(err);
};
if (!user) {
console.log("Incorrect username");
return done(null, false, { message: "Incorrect username" });
}
bcrypt.compare(password, user.password, (err, res) => {
if (res) {
// passwords match! log user in
console.log("Si pudiste logearte!!");
return done(null, user)
} else {
// passwords do not match!
console.log("Incorrect password");
return done(null, false, { message: "Incorrect password" })
}
})
});
})
);
router.post(
"/log-in",
passport.authenticate("local", {
failureFlash: false,
successRedirect: "/",
failureRedirect: "/log-in"
})
);