首页 > 解决方案 > 通过护照 jwt 进行身份验证并将 req 对象附加到对象名称“用户”

问题描述

通过passport-jwt进行身份验证时,它是否总是将req对象附加一个名为“user”的对象?“用户”这个名字是从哪里来的?

app.post('/profile', 
    passport.authenticate('jwt', { session: false }),
    function(req, res) {
            res.send(req.user.profile);
    }
);

此代码是否总是在对象后面附加req一个名为 的对象"user"?这个名字"user"是从哪里来的?

标签: node.jsexpresspassport.js

解决方案


这是passport.authenticate来自护照源代码第 12 行的定义。所以基本上,passport.authenticate它是一个中间件,它将进行数据提取,然后将数据绑定到req.user.

* Applies the `name`ed strategy (or strategies) to the incoming request, in
* order to authenticate the request.  If authentication is successful, the user
* will be logged in and populated at `req.user` and a session will be
* established by default.  If authentication fails, an unauthorized response

推荐阅读