Каков правильный синтаксис для постового вызова ax ios после django rest-auth токена в React JS? - PullRequest
0 голосов
/ 14 апреля 2020

В настоящее время мой код взаимодействует с моим django -rest-auth api. Он получает токен, но не аутентифицирует запрос. Мой бэкэнд показывает 200 (имя пользователя и пароль верны). В то время как мой интерфейс reactjs объявляет неверное имя пользователя или пароль (я создал в операторе else) и 400 неверных запросов.

 axios
      .post(
          'http://127.0.0.1:8000/rest-auth/login/',
          {
                username: this.state.username,
                password: this.state.password
          },
        //   { withCredentials: true }
          { isAuthenticated: true }
      )
          .then(response => {
              const token = response.data.key;
              if (localStorage.getItem(token)) {
                  console.log(token);
                  this.props.handleSucessfulAuth();
              } else {
                  this.setState({
                      errorMessage: 'Wrong username or password'
                  });
                  this.props.handleUnsuccessfulAuth();
                //   console.log(token);
              }

          })

       .catch(error => {
           this.setState({
               errorMessage: 'Authorization error occured',
               error
           })
           this.props.handleUnsuccessfulAuth();
    });

}

Теперь я изменил верхнюю часть на следующее:

handleSubmit(event) {
        event.preventDefault();
        axios.defaults.headers.common["Authorization"] = `Token ${this.props.token}`;

        axios
          .post(
              'http://127.0.0.1:8000/rest-auth/login/',
              {
                    username: this.state.username,
                    password: this.state.password
              },
            //   { withCredentials: true }
              { isAuthenticated: true }
          )
              .then(response => {
                  const token = response.data.key;
                  if (localStorage.setItem(token, 'token')) {
                      console.log(token);
                      this.props.handleSucessfulAuth();
                  } else {
                      this.setState({
                          errorMessage: 'Wrong username or password'
                      });
                      this.props.handleUnsuccessfulAuth();
                    //   console.log(token);
                  }

              })
...