我的auth应用不会将数据保存到会话

发布于 2025-01-26 20:49:14 字数 1196 浏览 5 评论 0原文

使用Express Mongoose和Passport JS创建验证应用程序。 使用NodeMailer Server添加邮件验证后,从Req.user添加用户到res.locals.currentuser可以从中间件中看到。在添加电子邮件验证之前,它可以正常运行。

    app.use(express.urlencoded({ extended: true }));
app.use(session(sessionObject));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));

passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.use((req, res, next) => {
  res.locals.currentUser = req.user;
  res.locals.success = req.flash("success");
  res.locals.error = req.flash("error");
  next();
});
app.get("/", (req, res) => {
  console.log(res.locals);
  res.render("index");
});
app.get("/login", (req, res) => res.render("login"));
app.post(
  "/login",
  passport.authenticate("local", {
    failureFlash: true,
    failureRedirect: "/login",
  }),
  (req, res) => {
    req.flash("success", "Welcome Back!");
    res.redirect("/");
    console.log(req.user);
  }
);

Creating Auth application using express mongoose and passport js.
after adding mail verification using nodemailer server sptoped adding user from req.user to res.locals.currentUser can be seen from middleware. before adding email verification it was functioning correctly.

    app.use(express.urlencoded({ extended: true }));
app.use(session(sessionObject));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));

passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.use((req, res, next) => {
  res.locals.currentUser = req.user;
  res.locals.success = req.flash("success");
  res.locals.error = req.flash("error");
  next();
});
app.get("/", (req, res) => {
  console.log(res.locals);
  res.render("index");
});
app.get("/login", (req, res) => res.render("login"));
app.post(
  "/login",
  passport.authenticate("local", {
    failureFlash: true,
    failureRedirect: "/login",
  }),
  (req, res) => {
    req.flash("success", "Welcome Back!");
    res.redirect("/");
    console.log(req.user);
  }
);

source code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So要识趣 2025-02-02 20:49:14

问题是:我在本地环境中工作,并将会话cookie设置为secure

由于Localhost在HTTP而不是HTTPS上运行,因此浏览器在重定向后没有将Session Cookie发送到应用程序,这阻止了Passport加载会话数据并创建req.user键。

The problem was: I was working in a local environment and had set the session cookie to secure.

Since Localhost runs on HTTP and not HTTPS, the browser was not sending the session cookie to the Application after the App redirected, which prevented Passport from loading the session data and creating the req.user key.

镜花水月 2025-02-02 20:49:14

reqres在请求结束时的所有内容,仅req.session写入会话商店(session> Session> Session object < /code>),然后从那里转到下一个请求。

如果要将当前用户保留在会话中,请将其写入req.session.currentuser。然后,您需要Passport.jsreq.user仅在建立会话的第一个请求中。

Everything in req and res expires at the end of the request, only req.session is written to the session store (sessionObject) and from there carried over to the next request.

If you want to keep the current user in the session, write it to req.session.currentUser. Then you need passport.js and req.user only during the first request, where the session is established.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文