How to update req.user session object set by passportjs? How to update req.user session object set by passportjs? express express

How to update req.user session object set by passportjs?


This works and persists for me:

req.session.passport.user.nickname = 'mynewnickname'

Are you employing caching on your routes that might need busting before refresh?

(passport serialize is the same)

passport.serializeUser(function(user, done) { done(null, user); });passport.deserializeUser(function(obj, done) { done(null, obj); });


I got the same problem and finally I found the solution:

var user = newDataObj;req.logIn(user, function(error) {    if (!error) {       console.log('succcessfully updated user');    }});res.end(); // important to update session

The req.logIn will call serialize function and save a new session to db.res.end() is important. without it the session will not be saved.


For 'serializeUser' you are returning the entire user... which will serialize the entire object and put it inside the cookie used to track sessions. That serialization only happens one time (when the session is established). When the next request comes in, it always deserializes that exact same user object you stored originally, which doesn't have any updates you made to it. That's why logging out and logging back in works, because it retrieves the edited user from your database (I assume), and recreates the session tracking cookie.

To confirm this answer, put a breakpoint on 'serializeUser', and notice that it is only hit when you login.