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

passport.js: remember me doesn't work in express app

我在我的申请中实施了本地护照策略。现在我要实现记住我的功能:

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