# withAuthenticationRequires returns user to another route after page refresh

**URL:** <https://community.auth0.com/t/withauthenticationrequires-returns-user-to-another-route-after-page-refresh/62227>\
**Category:** Get Help\
**Tags:** spa, react, login\
**Created:** [May 5, 2021, 2:57am UTC](https://community.auth0.com/t/withauthenticationrequires-returns-user-to-another-route-after-page-refresh/62227 "2021-05-05T02:57:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlperenOzturk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alperenozturk/32/29171_2.png) [@AlperenOzturk](https://community.auth0.com/u/AlperenOzturk)\
**Post date:** [May 5, 2021, 2:57am UTC](https://community.auth0.com/t/withauthenticationrequires-returns-user-to-another-route-after-page-refresh/62227/1 "2021-05-05T02:57:35Z")

</div>

Hey there,

I’m having an issue with the withAuthenticationRequired HOC. If I refresh a ProtectedRoute page it returns me to redirectUri page even if I declare the returnTo parameter.

Here is a quick summary of my code:

> index.js

```
ReactDOM.render(
    <BrowserRouter>
        <Auth0Provider domain="xx" clientId="xx" redirectUri='http://127.0.0.1:3000/profile' audience="xx" scope="xx">
            <App />
        </Auth0Provider>
    </BrowserRouter>,
    document.getElementById('root')
);

```

> Main.jsx

```
const Main = () => {
    return (
        <Switch>
            <Route exact path="/" component={Home} />
            ...
            <ProtectedRoute path="/credentials" component={Credentials} />
        </Switch>
    )
}

```

> protected-route.jsx

```
const ProtectedRoute = ({ component, ...args }) => (
  <Route
    component={withAuthenticationRequired(component, {
      onRedirecting: () => <Redirection />,
      returnTo: window.location.origin
    })}
    {...args}
  />
);

```

What am I doing wrong? How can I fix this? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 5, 2021, 12:58pm UTC](https://community.auth0.com/t/withauthenticationrequires-returns-user-to-another-route-after-page-refresh/62227/3 "2021-05-05T12:58:10Z")

</div>

Hi @AlperenOzturk,

Looking at the example [The Complete Guide to React User Authentication with Auth0](https://auth0.com/blog/complete-guide-to-react-user-authentication/), it looks like you can use the `onRedirectCallback` property of the `Auth0Provider` to handle the redirect:

```auto
import React from "react";
import { Auth0Provider } from "@auth0/auth0-react";
import { withRouter } from "react-router-dom";

class Auth0ProviderWithHistory extends React.Component {
  domain = process.env.REACT_APP_AUTH0_DOMAIN;
  clientId = process.env.REACT_APP_AUTH0_CLIENT_ID;
  audience = process.env.REACT_APP_AUTH0_AUDIENCE;

  onRedirectCallback = (appState) => {
    this.props.history.push(appState?.returnTo || window.location.pathname);
  };

  render() {
    return (
      <Auth0Provider
        domain={this.domain}
        clientId={this.clientId}
        redirectUri={window.location.origin}
        onRedirectCallback={this.onRedirectCallback}
        audience={this.audience}
      >
        {this.props.children}
      </Auth0Provider>
    );
  }
}

export default withRouter(Auth0ProviderWithHistory);

```

The `returnTo` prop of the `withAuthenticationRequired` component works alongside the `onRedirectCallback`. If you don’t have an `onRedirectCallback` prop, then I don’t believe the prop will do anything.

If you use the `onRedirectCallback` example above, then you shouldn’t need to use the `returnTo` prop unless you want to redirect somewhere other then the link the user clicked. For example, if a user clicked on the protected `/profile` link in the app, you could either leave out the `returnTo` prop from `withAuthenticationRequired` to have them land on `/profile` after they log in, or you could include the `returnTo` prop to have them land somewhere else after login (e.g. `/welcome-back`).

---

<div class="post-metadata">

**Author:** ![AlperenOzturk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alperenozturk/32/29171_2.png) [@AlperenOzturk](https://community.auth0.com/u/AlperenOzturk)\
**Post date:** [May 6, 2021, 2:29pm UTC](https://community.auth0.com/t/withauthenticationrequires-returns-user-to-another-route-after-page-refresh/62227/4 "2021-05-06T14:29:13Z")

</div>

I don’t understand the usage of onRedirectCallback exactly so I couldn’t get it to work. I would be glad if someone with a bit knowledge on this could explain further out.

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 6, 2021, 3:17pm UTC](https://community.auth0.com/t/withauthenticationrequires-returns-user-to-another-route-after-page-refresh/62227/5 "2021-05-06T15:17:06Z")

</div>

Hi @AlperenOzturk,

Here is the documentation for `onRedirectCallback`: [https://auth0.github.io/auth0-react/interfaces/auth0\_provider.auth0provideroptions.html#onredirectcallback](https://auth0.github.io/auth0-react/interfaces/auth0_provider.auth0provideroptions.html#onredirectcallback)

> By default this removes the code and state parameters from the url when you are redirected from the authorize page. It uses `window.history` but you might want to overwrite this if you are using a custom router, like `react-router-dom`

This function runs after authentication to handle the redirect callback.

You can find a full example of overriding the default behavior specifically for React Router in this article:

> **[The Complete Guide to React User Authentication with Auth0](https://auth0.com/blog/complete-guide-to-react-user-authentication/?_ga=2.5235254.464884123.1620041333-1595415333.1607347674)**
>
> Learn how to add user authentication to React using Context and Hooks. Learn the core concepts of React authentication.

Here is another example that is referenced in the documentation: [https://github.com/auth0/auth0-react/tree/9a15c540fee6d53514eaaea77cbadfb767b14891/examples/cra-react-router](https://github.com/auth0/auth0-react/tree/9a15c540fee6d53514eaaea77cbadfb767b14891/examples/cra-react-router)

Let me know if you have additional questions regarding `onRedirectCallback`. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [May 21, 2021, 3:17pm UTC](https://community.auth0.com/t/withauthenticationrequires-returns-user-to-another-route-after-page-refresh/62227/6 "2021-05-21T15:17:10Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
