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();
};
I'm using Postman to test my API. I setup the header with a x-access-token
key and the value superSuperSecret
. I got the error {"name":"JsonWebTokenError","message":"jwt malformed","level":"error"}
. I'm using this https://github.com/FortechRomania/express-mongo-example-project/blob/master/src/middlewares/validateToken.js
token
you are getting after console? – DenationalizesuperSuperSecret
– Chester