在会话中更新用户详细信息 [英] Update logged in user details in session

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

问题描述

我正在使用PassportJS与ExpressJS。



我需要更新登录的用户详细信息。虽然我在数据库中更新了这个,但是如何在会话中进行更新呢?request.user包含更新的用户详细信息?



更新数据库,如何更新用户的会话信息?



我尝试直接将更新的详细信息分配给 request.user 但它没有工作。
然后尝试 request.session.passport.user - 这是有效的,但是在request.user更新之前有5到10秒的延迟。



有没有一个我需要调用的功能来更新存储在会话中的用户信息?或者还有一些其他的对象,我可以更新哪里的变化没有延迟

解决方案

我一直在追捕这也是答案。没有在任何文档或教程中提到!



似乎有效的是,保存新更新的用户后,请执行 req.login(user) ...

  //user是新更新的信息
用户.save(function(err){
if(err)return next(err)
//护照会话发生了什么?检查一个特定的字段...
console.log(Before relogin:+ req.session.passport.user.changedField)

req.login(user,function(err){
if(err)return next(err)

console.log(After relogin:+ req.session.passport.user.changedField)
res.send(200)
})
})

线索在这里... https://github.com/jaredhanson/passport/issues/208


I am using PassportJS with ExpressJS.

I need to update the logged in user details. While I do update this in the DB, how do I update it in the session too so that request.user contains the updated user details?

That is, after updating the database, how do I update the session info on the user as well?

I tried directly assigning the updated details to request.user but it did not work. I then tried request.session.passport.user - this worked but there is a delay of around 5 to 10 seconds before it gets updated in request.user too.

Is there a function that I need to call that updates the user information stored in the session? Or is there some other object that I can update where the change does not have a delay

解决方案

I've been hunting down an answer for this too. Never mentioned in any docs or tutorials!

What seems to work is, after saving your newly updated user, do req.login(user)...

// "user" is the user with newly updated info
user.save(function(err) {
    if (err) return next(err)
    // What's happening in passport's session? Check a specific field...
    console.log("Before relogin: "+req.session.passport.user.changedField)

    req.login(user, function(err) {
        if (err) return next(err)

        console.log("After relogin: "+req.session.passport.user.changedField)
        res.send(200)
    })
})

The clue was here... https://github.com/jaredhanson/passport/issues/208

这篇关于在会话中更新用户详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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