如何在使用jwt时从解码的用户信息中访问用户ID?

时间:2017-09-01 08:26:32

标签: node.js authentication jwt

如何从jwt令牌中提取用户ID?我得到了未定义的'当我试图访问' id' “解码”中的属性'对象

我的代码 -

verifyToken: function(req, res, next) {
    var token = req.body.token || req.query.token || req.headers['access-token'];

    if (token) {

        jwt.verify(token, config.secret, function(err, decoded) {

            if (err) {
                return res.json({ success: false, message: 'Failed to authencticate token' });
            } else {

                // I want user id here. docoded.id is giving undefined

                req.user = decoded;
                next();
            }
        });
    } else {
        res.status(403).send({
            success: false,
            message: 'No token provided'
        });
    }
}

0 个答案:

没有答案