javascript - 具有本机反应的问题身份验证承载
问题描述
连接正在工作,但是当我尝试在中间件打开的情况下从我的数据库中获取信息时,出现错误 401。有关信息,该请求适用于邮递员,但不适用于我的应用程序。
我认为问题来自我获取的身份验证承载
我的中间件:
/* Middleware */
const authentification = (req, res, next) => {
try {
/* decode token and compare, set userID */
const token = req.headers.authorization.split(" ")[1];
const decodedToken = jwt.verify(token, "RANDOM_TOKEN_SECRET");
const userId = decodedToken.userId;
/* if userID in body compare with DB userID, else (no userID in body) it's ok*/
if (req.body.userId && req.body.userId !== userId) {
throw "Invalid user ID";
} else {
User.findOne({ _id: userId }, (err, data) => {
if (err) {
res.status(500).json({
error: new Error("Internal server error"),
});
return;
}
if (!data) {
res.status(404).json({
message: "Erreur d'authentification",
});
return;
}
req.user = data;
next();
});
}
} catch {
res.status(401).json({
message: "Invalid request!",
});
}
};
还有我的
getClubUser = async () => {
const headers = new Headers({
'Content-type': 'application/json',
Authorization: 'bearer' + (await AsyncStorage.getItem('token')),
});
const options = {
method: 'GET',
headers: headers,
};
fetch('http://localhost:8080/dashboard/clubInfo', options)
.then((response) => {
console.log(response);
return response.json();
})
.then(
(data) => {
this.setState({sport: data});
console.log(this.state.sport.clubs);
},
(err) => {
console.log(err);
},
);
};
解决方案
推荐阅读
- css - 如何在单击时启动 css 动画并在第二次单击时反转运行动画?
- mysql - 无法识别使用 MAX 和 OVER PARTITION BY 的语法问题
- flutter - Flutter SnackBar 或 Toast 仅在每次 Hot Reload 后显示
- java - Spring @Value with arraylist split 并获得第一个值
- oracle - 计算在 Oracle 中分组的查询中的记录时返回错误
- amazon-s3 - AWS 多区域服务可用性和运营
- c# - 在c#中将字节数组或位图转换为svg
- html - 可访问性:看起来像带有单个面板的选项卡的设计模式
- excel - 公式数据之间的动态范围
- googletest - Googlemock EXPECT_CALL 失败并出现错误:'operator==' 不匹配(操作数类型为 'const XX'