title: Redirect user to intended url after login Reactjs
body: <p>I am working on a web app using Reactjs for my front end. I prevented users from accessing some pages excepted they are logged in. My problem is how to allow users access their intended url instead of redirecting them back to the home page which I currently do.</p>
  
  <p>My routes are</p>
  
  <pre><code>&lt;Switch&gt;
      &lt;Route path="/desired-page" component={requireAuth(DesiredPage)} /&gt;
      &lt;Route path="/new-page" component={requireAuth(NewPage)} /&gt;
  &lt;/Switch&gt;
  </code></pre>
  
  <p>My requireAuth.js is</p>
  
  <pre><code>export default function(ComposedComponent) {
    class Authenticate extends React.Component {
      componentDidMount() {
        if (!this.props.isAuthenticated) {
          this.props.addFlashMessage({
            type: 'error',
            text: 'You need to login to access this page'
          });
          this.context.router.history.push('/login');
        }
      }
      render() {
        return (
          &lt;ComposedComponent {...this.props} /&gt;
        )
      }
    }
  
    Authenticate.propTypes = {
      isAuthenticated: PropTypes.bool.isRequired,
      addFlashMessage: PropTypes.func.isRequired
    }
  
    Authenticate.contextTypes = {
      router:PropTypes.object.isRequired
    }
  
    function mapStateToProps(state) {
      return {
        isAuthenticated: state.auth.isAuthenticated
      }
    }
    return connect(mapStateToProps, { addFlashMessage })(Authenticate);
  }
  </code></pre>