【发布时间】:2014-07-13 09:29:01
【问题描述】:
我尝试了许多设置会话变量的方法,但都未能找到解决方案。更确切地说,问题是会话(custome)变量没有被传递给其他路由(因此它们可能没有存储在默认的 MemoryStore 中)。 所有的代码都可以在https://github.com/codexa/pictroid/blob/session/app.js
这是分配会话变量的代码部分 (https://github.com/codexa/pictroid/blob/session/app.js#L304-311)
console.log(req.session);
req.session.regenerate(function(){
// Store the user's primary key
// in the session store to be retrieved,
// or in this case the entire user object
req.session.user = "some user";
req.session.auth = true;
});
我知道这是异步的,并且在执行此操作时将执行后续代码,但使其同步并不能解决问题。
例如,我正在尝试在根目录路由 (https://github.com/codexa/pictroid/blob/session/app.js#L87-L88) 中检索自定义“req.session.user”
app.get('/', function(req, res) {
console.log(req.session.auth);
...
那么谁能告诉我我做错了什么以及如何解决它? 提前致谢。
【问题讨论】:
-
为什么需要重新生成会话?
-
您还为您的 cookie 设置了
secure: true,这意味着该 cookie 仅适用于 https。您的服务器似乎没有使用 https。 -
我正在重新生成 cookie,因为建议这样做(以防止固定 - github.com/visionmedia/express/blob/master/examples/auth/app.js)。所以在开发环境中,我不应该将secure设置为true?