title: Forcing login in react after 401 from backend
body: <p>I'd like to use react with following flow:</p>
  
  <ul>
  <li>user is doing something</li>
  <li>user tries to open page required authentication</li>
  <li>frontend sends request to server(ie get list of some objects)</li>
  <li>if user is authenticated server responds with list of objects, state is updated and everyone is happy</li>
  <li>if user is not authenticated server responds with 401 unauthorized, I want to show login form(page), perform login and redirect to originally requested page</li>
  </ul>
  
  <p>Thing that I have now is:</p>
  
  <p>App.js:</p>
  
  <pre><code>class App extends Component {
    render() {
      return (
        &lt;BrowserRouter&gt;
          &lt;Switch&gt;
            &lt;Route exact path="/login" name="Login Page" component={Login} /&gt;
            &lt;Route path="/private" name="Private page" component={PrivatePage}/&gt;
            &lt;Route path="/" name="Home" component={Index} /&gt;
  
          &lt;/Switch&gt;
        &lt;/BrowserRouter&gt;
      );
    }
  }
  </code></pre>
  
  <p>PrivatePage.js:</p>
  
  <pre><code>import api from '../../api';
  class PrivatePage extends Component {
    state = {
      first_name: '',
      last_name: ''
    };
    componentDidMount() {
        api.getProfile().then((res) =&gt; { /* this needs to be executed only if status code was 200 */
          this.setState({
            first_name: res.first_name,
            last_name: res.last_name
          })
        })
    }
  
    render() {
      return (
        &lt;div&gt;
          &lt;p&gt;first name: {this.state.first_name}&lt;/p&gt;
          &lt;p&gt;last name: {this.state.last_name}&lt;/p&gt;
        &lt;/div&gt;
      );
    }
  }
  </code></pre>
  
  <p>Api.js:</p>
  
  <pre><code>export class Api {
  
    getProfile(){
      return fetch('http://some_endpoint.com', {
          method: 'GET',
          body: '{}'
      })
      .then(/* probably something here needs to redirect to Login page in case of 401? */)
    }
  
  }
  let api = new Api();
  export default api;
  </code></pre>
  
  <p>This is of course simplified version of code- but shows my intention.</p>