node.js - React - 使用 await fetch 处理 jwt 身份验证失败
问题描述
我编写了一个 API 来做一些事情,并使用jwt
令牌保护它。这是检查有效性(checkAuth
)的功能:
module.exports = (req, res, next) => {
try {
const token = req.headers.authorization;
const decoded = jwt.verify(token, process.env.TOKEN_SECRET);
req.userData = decoded;
next();
}
catch (error) {
return res.status(401).json({
message: 'Auth failed'
});
}
};
我以这种方式定义route
of :API
router.post('/myAPI', checkAuth, MyAPIDefinition);
当我尝试获取 API 时,使用过期 jwt
的我无法获得checkAuth
. 这是我API
使用的电话await fetch
:
const response = await fetch(`${API_URI_DEV}/myAPI`, {
method: 'POST',
body: JSON.stringify({encod: { content: data }}),
headers: {
'Content-Type': 'application/json; charset=utf-8',
Accept: 'application/json',
'authorization': jwt
}
});
的内容response
是undefined
。
解决方案
推荐阅读
- r - 如何在 purr 中修复“eval 中的错误(predvars,data,env):数字 'envir' arg 长度不为 1”
- clojure - 如何在 clojure 中维护 2d 矢量的单个副本
- three.js - THREE.JS:所有轴上的粒子排斥 [XYZ]
- sql - SQL Server 触发器 case 语句未捕获
- vbscript - 添加输入文件日期检查时的代码性能
- javascript - 获取返回的 JSON 数据以在 vue charts.js 图表中使用的问题
- java - MyBatis中如何选择动态fetchSize
- javascript - 如何在 Edge 中识别自定义 CSS 复选框的值?
- shell - 防止在 Makefile 中执行命令 shell
- python - 为 LSTM 生成输入和输出