How can I force req.user to be updated?

I am using a .js passport with a local authentication strategy for my node.js / express application.

The user returned by LocalStrategy includes things such as email and username.

I want to give users the opportunity to update their email or username in the application. When this happens, I would like to inform the passport in order to restart the user (it seems as if they had just logged in), so that req.user reflects the updated changes in the remainder of the session. Just setting this up didn't seem to last in the last request.

A simplified example:

 app.get('/changeEmail', function(req, res, next) { var userId = req.user.id; var origEmail = req.user.email; var newEmail = req.param('email'); userDao.updateEmail(userId, newEmail, function(err) { if (!err) { // user email has changed, need change reflected in req.user from this point on req.user.email = newEmail; } next(); }); }); 
+7
session express
source share
1 answer

You should be able to call the logIn method in the request. This will call the serializeUser method to update the session.

 userDao.updateEmail(userId, newEmail, function(err) { if (!err) { // user email has changed, need change reflected in req.user from this point on var user = req.user; user.email = newEmail; req.logIn(user, function(error) { if (!error) { // successfully serialized user to session } }); } next(); }); 
+7
source share

All Articles