【发布时间】:2021-11-24 12:42:40
【问题描述】:
我正在尝试解析 Express 后端中的 URL,但是当我转到 localhost:3000/token=myJwtToken 之类的 URL 时,我在后端的 authJwt.verifyToken 中间件中得到 undefined for req.query.token。
在我的 React App.js 中,我正在访问以下端点,我需要在 URL 中验证一个令牌作为参数/查询字符串,其中包含发送方和接收方共享的秘密:
const req = {
url: 'http://localhost:8080/api/verify',
method: 'GET',
};
在我的后端我有这条路线:
module.exports = function (app) {
app.get(
'/api/verify',
[authJwt.verifyToken], // verify token in URL middleware
galleryController.loadGallery, // proceed to gallery
);
};
我的 verifyToken 身份验证中间件是这样的:
verifyToken = (req, res, next) => {
let token = req.query.token;
console.log(token); // undefined
console.log(req.params); // {}
if (!token) {
console.log('NO TOKEN!!!');
return res.status(401).send({
message: 'Unauthorized!',
});
}
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
if (err) {
console.log(`Error: ${err}`);
return res.status(401).send({
message: `Error: ${err}`,
});
}
console.log('success!', decoded.token);
next();
});
};
我哪里出错了?
【问题讨论】:
标签: node.js reactjs express jwt