Passport: req.user does not get updated

Created on 6 Feb 2013  路  1Comment  路  Source: jaredhanson/passport

After a successful authentication, passport,js sets req.user. But in some of my routes, i update the user object in mongodb database. When i try to reach it again from req.user, i get the outdated version. Doesn't passport.js automatically update the req.user object?

Or what is the appropriate way of doing this? ( I'm using express.js 3.x , mongodb )
Thanks

Most helpful comment

At last, i found where the problem is. I was not deserializing user :

passport.deserializeUser(function(obj, done) {
db.User.findOne(obj._id, function (err, user) {
done(err, user);
});
});

Now it is updated at every request. Also one minor thing that you can come across, if you don't want to hit db for deserializing the user object for static resources, just move the conf in app.js to up in the middleware order

app.use(express.static(path.join(__dirname, 'public')));

>All comments

At last, i found where the problem is. I was not deserializing user :

passport.deserializeUser(function(obj, done) {
db.User.findOne(obj._id, function (err, user) {
done(err, user);
});
});

Now it is updated at every request. Also one minor thing that you can come across, if you don't want to hit db for deserializing the user object for static resources, just move the conf in app.js to up in the middleware order

app.use(express.static(path.join(__dirname, 'public')));

Was this page helpful?
0 / 5 - 0 ratings

Related issues

angel1st picture angel1st  路  7Comments

xingrz picture xingrz  路  5Comments

callumacrae picture callumacrae  路  5Comments

wongeun picture wongeun  路  4Comments

fvgs picture fvgs  路  6Comments