【发布时间】:2019-01-21 18:12:39
【问题描述】:
const jwt = require("jsonwebtoken");
const SECRET = "superSuperSecret";
module.exports = function(req, res, next) {
const token = req.body.token || req.query.token || req.headers[ "x-access-token" ];
if (token) {
return jwt.verify(token, SECRET, function(err, decoded) {
if (err) {
return res.json({
success: false,
message: "Failed to authenticate token.",
});
}
req.user = decoded;
return next();
});
}
return res.unauthorized();
};
我正在使用 Postman 来测试我的 API。我使用x-access-token 键和值superSuperSecret 设置标题。我收到错误{"name":"JsonWebTokenError","message":"jwt malformed","level":"error"}。我正在使用这个https://github.com/FortechRomania/express-mongo-example-project/blob/master/src/middlewares/validateToken.js
【问题讨论】:
-
你在控制台之后得到的
token的值是多少? -
@SookieSingh
superSuperSecret -
另外我认为带有回调的 jwt.verfy 是异步函数
标签: node.js express jwt postman