PassportJS adding another property to req.user

情到浓时终转凉″ 提交于 2019-12-20 06:37:36

问题


How would I add a variable to the request.user property?

Most of my routes involve checking for the authenticated user via "req.userusername".

How could I add another field for location. I ask this because I want to add a location field. to the req.user object.


回答1:


Either in the deserialization function, before returning the user

passport.deserializeUser(function(id, done) {
    getUser(id).then(function(user) {
        user.whatever = 'you like';
        return done(null, user);
    });
});

or in an express middleware (before the router).

app.use(function(req, res, next) {
    if(req.user) req.user.whatever = 'you like';
    next();
});


来源:https://stackoverflow.com/questions/23505424/passportjs-adding-another-property-to-req-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!