PassportJS 向 req.user 添加另一个属性 [英] PassportJS adding another property to req.user

查看:69
本文介绍了PassportJS 向 req.user 添加另一个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何向 request.user 属性添加变量?

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

我的大多数路由都涉及通过req.userusername"检查经过身份验证的用户.

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

我如何为位置添加另一个字段.我问这个是因为我想添加一个位置字段.到 req.user 对象.

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

推荐答案

要么在反序列化函数中,在返回用户之前

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();
});

这篇关于PassportJS 向 req.user 添加另一个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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