微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有本机响应的问题身份验证承载

如何解决具有本机响应的问题身份验证承载

连接正在运行,但是当我尝试使用中间件打开数据库时从数据库获取信息时,出现错误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);
        },);
  };

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?