移动网站中的快速会话不会持续存在会话

session not persisting with express-session in mobile website

我有一个使用 NodeJS、Express 和 AngularJS.I 构建的 Web 应用程序,我正在使用 express-session 来维护登录会话。

在桌面浏览器中,正如预期的那样,即使在浏览器关闭后,登录会话仍然存在,但是

While accessing the website from mobile, the user gets automatically logged out every time the mobile browser is closed.

示例代码如下:

let express = require('express');
let app = express();
let session = require('express-session');
let MongoStore = require('connect-mongo')(session);

app.use(session({
    secret: config.sessionSecret,
    name: "test",
    store: new MongoStore({
        url: mongoConnectionUrl,
        ttl: 2 * 24 * 60 * 60, // = 2 days. Default 
        autoRemove: 'native'
    }),
    resave: true,
    saveUninitialized: true
}));

如何在手机浏览器关闭时停止手机用户自动注销?

面临上述问题的网站:https://ayan.work/notes

只需使用以下代码进行配置:

app.use(session({
    secret: config.sessionSecret,
    name: "test",
    cookie: { maxAge: 3 * 24 * 60 * 60 * 1000 }, //user won't have to login for 3 days
    store: new (require('express-sessions'))({
        storage: 'mongodb',
        instance: mongoose, // optional 
        host: 'localhost', // optional 
        port: 27017, // optional 
        db: 'database name', // optional 
        collection: 'sessions', // optional 
        expire: 86400 // optional 
    })
}));

选项 maxAge 将在 cookie 中保留数据。