Проблема с носителем аутентификации с помощью react native

#javascript #react-native #connexion

#javascript #react-native #подключение

Вопрос:

Соединение работает, но когда я попытался получить информацию из своей базы данных с включенным промежуточным программным обеспечением, у меня возникла ошибка 401. Для информации, запрос работает в postman, но не в моем приложении.

Я думаю, что проблема связана с носителем аутентификации в моей выборке

мое промежуточное программное обеспечение :

 /* 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 amp;amp; 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);
        },
      );
  };