title: Redirecting a user to the page they requested after successful authentication with react-router-dom
body: <p>I have constructed a Public route component for logging in to show up if the user is not authenticated. Whenever a user that is not logged clicks on a protected route, he will be redirected to the login page where he can enter the credentials. I want a programmatic way so that if he logged in with the correct credentials, he should be redirected to the page that he tried to access at the first place. For example if the user requested the profile page, he should be redirected to it after logging in, if the user requested the settings page, the same would happen. </p>
  
  <p>As of currently, I can only redirect them to the home path <code>/</code>. Is there any way I can use Redirect so that it knows the path the user requested?</p>
  
  <p>Here is my current code for the Public Route component</p>
  
  <pre><code>export const PublicRoute = ({
      isAuthenticated,
      component: Component,
      ...rest
  }: PublicRouteProps) =&gt; (
      &lt;Route
          {...rest}
          component={(props: any) =&gt; {
              console.log(props.path);
              return isAuthenticated.auth ? (
                  &lt;Redirect to='/' /&gt;
              ) : (
                  &lt;div&gt;
                      &lt;Component {...props} /&gt;
                  &lt;/div&gt;
              );
          }}
      /&gt;
  );
  const mapStateToProps = (state: ReduxStoreState) =&gt; ({
      isAuthenticated: state.isAuthenticated
  });
  
  export default connect(mapStateToProps)(PublicRoute);
  
  </code></pre>