签署jwt web令牌会返回null - nodejs

时间:2016-11-13 21:36:06

标签: node.js jwt access-token

jwt签名失败并为令牌返回null。这是我的实施:

var jwt = require('jsonwebtoken');

var jwt_session_secret = "rnwR_apwvkWjdHovcmmQuoDwLR8av9oFjbJm2KhS7oEX9aSiBu9e96jxCDM9vj5_x8OlCCEiXwIGkagL-KDgnDsulc2e0QU5qYN75lzomrc4P9gqgGXB7HVfpaBcKgW3oLsXRBWAoFT5ICsOSwT-70hdQtJfE7a3NK6j3jmxcSE";

var payload = { ud: 23 };
var exp = Math.floor((new Date(+new Date + 12096e5).getTime())/1000);
var opts = { issuer: 'gateway', expiresIn: exp };

console.log(payload);

jwt.sign( payload, jwt_session_secret, opts, function(token) {

    console.log("token = " + token);

        jwt.verify(token, jwt_session_secret, function(err, decoded) {
            if(err) {
                console.log("Error failed: " + err);
                return;
            }
            console.log("decoded");
            console.log(decoded);
            });
        });

代码看起来不错,但不确定我是否遗漏了一些东西。这是输出:

{ ud: 23 } token = null Error failed: JsonWebTokenError: jwt must be provided

1 个答案:

答案 0 :(得分:1)

您需要在回调中接受两个参数,错误令牌,如下所示:

jwt.sign( payload, jwt_session_secret, opts, function(err, token) {