更新快速会话 [英] Updating express-session sessions

查看:50
本文介绍了更新快速会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想做的是,当用户访问该页面时,它将从mongodb数据库中获取他们的用户配置文件,然后使用用户配置文件更新用户会话.

So what I'm trying to do is when the users accesses the page it will fetch their user profile from the mongodb database and then update the users session with the users profile.

现在,我目前正在使用以下程序包进行会话:
-express-socket.io-session(允许我通过socket.io访问cookie)
-快速会话
-connect-mongo(允许我将会话存储在mongodb数据库中)

Right now I'm currently using the following packages for sessions:
- express-socket.io-session (allows me to access cookies through socket.io)
- express-session
- connect-mongo (Allows me to store sessions in a mongodb database)

在这里,我尝试使用这些方法来更新用户的Cookie

Heres what I've tried to use to get the users cookies to update

req.session.reload(function(err) {
  // session updated 
})

推荐答案

通常,您将对象(例如用户个人资料)保存在会话中,并且会话存储在mongo中.
客户端上的Cookie仅包含过期日期和会话ID.对于每个请求,您都会获得带有会话ID的cookie,并可以通过此ID从mongo获取会话.
当您更新会话中的任何字段时,它将在mongo中重新保存.
因此,您不需要重新加载".您可以像 req.session.user = user 这样分配.
文档

Typically you save object (e.g user profile) in session and session is stored in mongo.
Cookie on client contains only expires date and session id. With each request you get cookie with session ID and can obtain session from mongo by this ID.
When you update any field in session it will be resaved in mongo.
So you don't need 'reload'. You can just assign in like req.session.user = user .
Docs

这篇关于更新快速会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆