如何在浏览器中消费flash message?护照,Node.js,content-flash

How to consume flash message in the browser? Passport, Node.js, content-flash

我正在学习(相当挣扎),Passport 模块。我在我的“策略”中指定了 flash 消息,如下所示:

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"));
    }
  );

我在文档中读到这些闪现消息可用于通知用户身份验证状态:

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.

问题是我不知道如何在后端和前端访问这些即显消息。

您可以使用请求参数 (req) 访问即显消息。

app.get('/User', function (req, res) {
  res.render('User', { message: req.flash('message') });
});