如何让用户会话无限期地保持活动状态?
How can I keep user sessions alive indefinitely?
我有 Nodejs、ReactJS 和 React Native 应用程序,MERN 应用程序使用免费的 dynos 部署在 Heroku 上。会话会在一段时间后自动超时。我希望永远保持会话没有过期。
我在初始化会话时没有设置任何 expiry time
或 MaxAge
:
app.use(session({
secret: 'thesecret',
saveUninitialized: false,
resave: false
}))
app.use(passport.initialize());
app.use(passport.session());
我希望在我的 ReactJS 和 React Native 应用程序中永远保持会话。我怎样才能在 Heroku 上做到这一点?
您似乎没有配置 store
, which means you're using the default MemoryStore
:
Warning The default server-side session storage, MemoryStore
, is purposely not designed for a production environment. It will leak memory under most conditions, does not scale past a single process, and is meant for debugging and developing.
For a list of stores, see compatible session stores.
Heroku dynos restart frequently,每天至少一次,以保持应用程序和基础架构的健康,即使没有事先警告,内存存储也不实用。
您将不得不选择一个新的 session store and configure it. Heroku's filesystem is ephemeral, so please avoid anything that stores its data on the local disk. It probably makes sense to pick one that uses a data store listed among Heroku's addons. One of the PostgreSQL backends might be a good choice (you probably already have a Heroku Postgres database),但是有 很多 的选择。
通过在 mongodb 数据库中保存会话,我们可以在我们的 nodeJS 应用程序中保持连续的会话。
var MongoDBStore = require('connect-mongodb-session')(session);
var store = new MongoDBStore({
uri: process.env.MONGODB_URI,
collection: 'mySessions'
});
// Catch errors
store.on('error', function(error) {
console.log(error);
});
app.use(session({
secret: 'thesecret',
saveUninitialized: false,
resave: false,
cookie: {expires: new Date(1586323351000),maxAge:31536000000},
store: store,
}))
我有 Nodejs、ReactJS 和 React Native 应用程序,MERN 应用程序使用免费的 dynos 部署在 Heroku 上。会话会在一段时间后自动超时。我希望永远保持会话没有过期。
我在初始化会话时没有设置任何 expiry time
或 MaxAge
:
app.use(session({
secret: 'thesecret',
saveUninitialized: false,
resave: false
}))
app.use(passport.initialize());
app.use(passport.session());
我希望在我的 ReactJS 和 React Native 应用程序中永远保持会话。我怎样才能在 Heroku 上做到这一点?
您似乎没有配置 store
, which means you're using the default MemoryStore
:
Warning The default server-side session storage,
MemoryStore
, is purposely not designed for a production environment. It will leak memory under most conditions, does not scale past a single process, and is meant for debugging and developing.For a list of stores, see compatible session stores.
Heroku dynos restart frequently,每天至少一次,以保持应用程序和基础架构的健康,即使没有事先警告,内存存储也不实用。
您将不得不选择一个新的 session store and configure it. Heroku's filesystem is ephemeral, so please avoid anything that stores its data on the local disk. It probably makes sense to pick one that uses a data store listed among Heroku's addons. One of the PostgreSQL backends might be a good choice (you probably already have a Heroku Postgres database),但是有 很多 的选择。
通过在 mongodb 数据库中保存会话,我们可以在我们的 nodeJS 应用程序中保持连续的会话。
var MongoDBStore = require('connect-mongodb-session')(session);
var store = new MongoDBStore({
uri: process.env.MONGODB_URI,
collection: 'mySessions'
});
// Catch errors
store.on('error', function(error) {
console.log(error);
});
app.use(session({
secret: 'thesecret',
saveUninitialized: false,
resave: false,
cookie: {expires: new Date(1586323351000),maxAge:31536000000},
store: store,
}))