gpt4 book ai didi

node.js - Passport.js:记住我在 Express 应用程序中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:59 24 4
gpt4 key购买 nike

我在我的应用程序中实现了 Passport 本地策略。现在我要实现记住我的功能:

app.use(passport.initialize());
app.use(passport.session());

app.post("/login", function (req, res, next) {
var authFunction = passport.authenticate("local", function (err, user, info) {
if (err) {
next(err);
} else {
req.logIn(user, function (err) {
if (err) {
next(err);
} else {
if (req.body.remember) {
req.session.cookie.originalMaxAge = 2592000000;
} else {
req.session.cookie._expires = false;
}
res.redirect("/");
}
});
}
});
authFunction(req, res, next);
});

如您所见,我设置了 originalMaxAge,但当我关闭并再次打开浏览器时它不起作用。
有什么想法吗?

最佳答案

来自Connect Documentation :

Session#cookie

Each session has a unique cookie object accompany it. This allows you to alter the session cookie per visitor. For example we can set req.session.cookie.expires to false to enable the cookie to remain for only the duration of the user-agent.

Session#maxAge

Alternatively req.session.cookie.maxAge will return the time remaining in milliseconds, which we may also re-assign a new value to adjust the .expires property appropriately. The following are essentially equivalent

根据此,您必须设置 expiresmaxAge 属性:

if (req.body.remember) {
var oneHour = 3600000;
req.session.cookie.expires = new Date(Date.now() + hour);
req.session.cookie.maxAge = hour;
} else {
req.session.cookie.expires = false;
}

关于node.js - Passport.js:记住我在 Express 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37204091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com