I am studying (rather struggling), Passport module. I have specified flash messages in my "Strategy" like this:
passport.use(
new Strategy(function(username, password, cb) {
if (username !== "riko") {
// console.log("Incorrect User");
return cb(null, false, { message: "Icorrect user!" });
}
if (password !== "kote") {
// console.log("Incorrect Password");
return cb(null, false, { message: "Icorrect password!" });
}
return cb(null, { username: "riko", passpord: "kote", chemer: "memer" });
})
);
app.post(
"/Login",
passport.authenticate("local", {
successRedirect: "/User",
failureFlash: true
}),
function(req, res) {
console.log("LOGIN POST!");
res.redirect("/Home");
// res.sendFile(path.join(__dirname, "client/build", "index.html"));
}
);
I read in the documentation that these flash messages can be used to inform the user with authentication status:
Redirects are often combined with flash messages in order to display status information to the user.
Setting the failureFlash option to true instructs Passport to flash an error message using the message given by the strategy's verify callback, if any. This is often the best approach, because the verify callback can make the most accurate determination of why authentication failed.
The problem is that I don't know how to access these flash messages neither in the backend nor frontend.
You can access flash messages using the request parameter (req
).
app.get('/User', function (req, res) {
res.render('User', { message: req.flash('message') });
});