app.post('/doLogin', function(req,res){
db.users.findOne({username: req.body.username}, function(err, user) {
if( err ) {
console.log("Login fail");
}
else if (user != null) {
if (req.body.password == user.password) {
req.session.user_role = "user";
req.session.save();
} else {
req.session.user_role = "null";
console.log("Wrong login");
}
}
res.send({redirect: "/"});
});
});
该函数用于将一个变量保存到会话中 req.session.user_role = "user";
但是当新的请求检查用户是否已登录时
app.get('/', function(req,res){
redis.get('sess:' + req.session.id, function(err, result){
console.log("Get session: " + util.inspect(JSON.parse(result),{ showHidden: true, depth: null }));
});
if ((req.session.user_role == "user")) {
console.log("Logged in");
} else {
console.log("Logged out");
}
});
每次返回的结果都是“已注销”,因为会话被更改了。我使用Redis存储会话,我认为是Redis的问题,因为当我停止使用Redis时,一切正常。
{ path: '/', httpOnly: true, secure: false, maxAge: null }
。这个点上文档是错误的还是我漏掉了什么? - christophrus