# Implementing Auth0 Protected Routes using React Router v6...?

**URL:** <https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667>\
**Category:** Get Help\
**Tags:** react, protected, auth0\
**Created:** [December 31, 2021, 12:13pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667 "2021-12-31T12:13:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![robliou01](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/robliou01/32/34126_2.png) [@robliou01](https://community.auth0.com/u/robliou01)\
**Post date:** [December 31, 2021, 12:13pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/1 "2021-12-31T12:13:02Z")

</div>

Hello Friends,

I’m wondering if there’s anyone who could please provide a resource on how to implement Auth0 Protected Route functionality using React Router v6?

The Auth0 React [tutorial](https://auth0.com/blog/complete-guide-to-react-user-authentication/#Protecting-Routes) that currently exists, while very while authored, appears to be for React Router version 5 or earlier. Hence, it does not appear to work for those of us who are using v6. I have tried to make the logical updates that one would expect going from RR v5 to v6, such as using “Navigate to” instead of “Redirect,” using “element=” instead of “component=”, and changing how I wrap routes using “ProtectedRoute”, but I still cannot get things to work properly…

The errors I receive when trying to implement Protected Routes using the guide vary from  
_“A is only ever to be used as the child of element, never rendered directly. Please wrap your in a . protected route”_ to

“Error: [ProtectedRoute] is not a component. All component children of must be a or \<React.Fragment\>”

The errors vary depending on what change I’m attempting to implement in going from v5 to v6 using the current guide.

If anyone has a code sample or guide on how to use Auth0 with RR v6, I would be very grateful. Thanks…!

---

<div class="post-metadata">

**Author:** ![robliou01](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/robliou01/32/34126_2.png) [@robliou01](https://community.auth0.com/u/robliou01)\
**Post date:** [December 31, 2021, 12:32pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/2 "2021-12-31T12:32:09Z")

</div>

Note- I just saw in the comments for the Auth0-React guide that the author (Dan) is planning to update the guide for React Router v6 in early 2022. Therefore, I will be patient and happily await his updates!

---

<div class="post-metadata">

**Author:** ![jcespinoza](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jcespinoza/32/32974_2.png) [@jcespinoza](https://community.auth0.com/u/jcespinoza)\
**Post date:** [January 7, 2022, 8:07pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/3 "2022-01-07T20:07:53Z")

</div>

Hey Rob,

In my team we recently ran into this problem and our strategy goes something like this:

```auto
const App => 
  <Routes>
    <Route path="some-path" element={<ProtectedComponent component={SomePrivateView} aProps={prop1} anotherProp={prop2} />} />
    ...more routes
  </Routes>

```

`SomePrivateView` is the component function. i.e.:

```auto
const SomePrivateView = (props) => <>something</>

```

In `ProtectedComponent` we let `withAuthenticationRequired` to the redirection if needed:

```auto
const ProtectedComponent = ({ component, ...propsForComponent}) => {
  const Cp = withAuthenticationRequired(component);
  return <Cp {...propsForComponent} />
}

```

For the moment, this has been enough. Hope this helps someone also looking to upgrade.

Regards,  
Juan Carlos

---

<div class="post-metadata">

**Author:** ![lolmos](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lolmos/32/34322_2.png) [@lolmos](https://community.auth0.com/u/lolmos)\
**Post date:** [January 11, 2022, 11:39am UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/4 "2022-01-11T11:39:17Z")

</div>

Thanks Juan Carlos this works a treat!

---

<div class="post-metadata">

**Author:** ![robliou01](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/robliou01/32/34126_2.png) [@robliou01](https://community.auth0.com/u/robliou01)\
**Post date:** [January 12, 2022, 7:47am UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/5 "2022-01-12T07:47:04Z")

</div>

@jcespinoza

Apologies sir for the late response, as I got locked up in another, totally unrelated area.

Indeed - thank you for posting this. This is great, and it works…!

Just as a follow- up question… do you or anyone else happen to have any idea on how to do a redirect to another page in the case of the login attempt failing?

Auth0 appears to use a module called withAuthenticationRequired() to handle this, but it’s sort of a blackbox to me, and I can’t really find any detailed documentation on how it works. Therefore, I have no idea how to set the redirect page to something else…? The example given in the React guide is for React Router v5 only…

Thanks,

---

<div class="post-metadata">

**Author:** ![jcespinoza](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jcespinoza/32/32974_2.png) [@jcespinoza](https://community.auth0.com/u/jcespinoza)\
**Post date:** [January 27, 2022, 4:40am UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/6 "2022-01-27T04:40:21Z")

</div>

Speaking of late responses 🤦‍♂️

So, when you attempt to login and fail, don’t we just get locked in that page?  
One idea, if I had to navigate away from the login after a failing attempt is, I would customize Auth0’s login with my html, `catch` the exception and redirect somewhere else from there.

```javascript
      function login(e) {
        e.preventDefault();
        var button = this;
        var username = document.getElementById("email").value;
        var password = document.getElementById("password").value;

        webAuth.login(
          {
            realm: databaseConnection,
            username: username,
            password: password,
            captcha: captcha.getValue(),
          },
          function (err) {
            if (err) {
              /* REDIRECT SOMEWHERE FROM HERE */
              window.location.replace("https://yourpage");
            }
          },
        );
      }

```

I know this has nothing to do with React Router so maybe I misunderstood the question, so feel free to link to those examples you mention.

Regards,  
Juan Carlos

---

<div class="post-metadata">

**Author:** ![holm.markus](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/holm.markus/32/34804_2.png) [@holm.markus](https://community.auth0.com/u/holm.markus)\
**Post date:** [January 31, 2022, 2:29pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/7 "2022-01-31T14:29:39Z")

</div>

Doing the same thing in another way. In my opinion a bit clearer.

```auto
cost SomePrivateView = () => {

}

export default withAuthenticationRequired(SomePrivateView)

```

---

<div class="post-metadata">

**Author:** ![dan-auth0](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@dan-auth0](https://community.auth0.com/u/dan-auth0)\
**Post date:** [February 9, 2022, 2:30am UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/8 "2022-02-09T02:30:09Z")

</div>

@robliou01 @jcespinoza @lolmos @holm.markus I created a code sample for React Router 6 + React 17:

> **[React Router 6 Code Sample: Basic Authentication](https://developer.auth0.com/resources/code-samples/spa/react/basic-authentication)**
>
> Code sample showing how to protect a simple React single-page application using React Router 6, Auth0, and JavaScript.

I invite you to try it out, please 🙂 I will be gathering feedback and then building the new React Authentication guide based on it 👀

Right now it uses TypeScript but I’ll push a JavaScript version next week after I get some feedback 🙂 It’s easier to iterate using TypeScript hehe

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [February 9, 2022, 1:29pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/9 "2022-02-09T13:29:26Z")

</div>

Thanks for contributing to this thread and sharing it here Dan!

---

<div class="post-metadata">

**Author:** ![robliou01](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/robliou01/32/34126_2.png) [@robliou01](https://community.auth0.com/u/robliou01)\
**Post date:** [February 9, 2022, 2:08pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/10 "2022-02-09T14:08:31Z")

</div>

Thanks, Dan!

I’m currently busy trying to implement JWT access token between my Auth0 and Hasura GraphQL app setup. I see that this will all together with your tutorial. I’ll try to get to it as soon as I can, with feedback.

Thanks again,

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [February 10, 2022, 11:19am UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/11 "2022-02-10T11:19:10Z")

</div>

Take your time @robliou01 !

---

<div class="post-metadata">

**Author:** ![robliou01](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/robliou01/32/34126_2.png) [@robliou01](https://community.auth0.com/u/robliou01)\
**Post date:** [February 22, 2022, 2:19pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/12 "2022-02-22T14:19:12Z")

</div>

> [@dan-auth0](#):
>
> ck 🙂 It’s easier to iterate using TypeScript hehe

Dan, thanks for updating the guide!

Looks like you’ve covered the part about how to protect routes in RRv6. The only other part that I noticed would be helpful if updated is:

How do we make/update the Profile page to be conditional on whether login is successful or not?

If I have more specific questions, I will be sure to contact support or upgrade my support level…

Thanks,

Rob

---

<div class="post-metadata">

**Author:** ![dan-auth0](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@dan-auth0](https://community.auth0.com/u/dan-auth0)\
**Post date:** [February 23, 2022, 6:53pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/13 "2022-02-23T18:53:27Z")

</div>

> [@robliou01](#):
>
> How do we make/update the Profile page to be conditional on whether login is successful or not?

On the current code sample, you need to be logged in in order to access the profile page, `/profile`. Is that different from your use case?

---

<div class="post-metadata">

**Author:** ![robliou01](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/robliou01/32/34126_2.png) [@robliou01](https://community.auth0.com/u/robliou01)\
**Post date:** [March 4, 2022, 5:03am UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/14 "2022-03-04T05:03:44Z")

</div>

Dan,

This contains what I was looking for:

> **[React Router 6 Code Sample: Basic Authentication](https://developer.auth0.com/resources/code-samples/spa/react/basic-authentication)**
>
> Code sample showing how to protect a simple React single-page application using React Router 6, Auth0, and JavaScript.

Thanks!

R

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [March 4, 2022, 7:40am UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/15 "2022-03-04T07:40:27Z")

</div>

Thanks for sharing with the rest of community!

---

<div class="post-metadata">

**Author:** ![dan-auth0](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@dan-auth0](https://community.auth0.com/u/dan-auth0)\
**Post date:** [March 4, 2022, 3:43pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/16 "2022-03-04T15:43:16Z")

</div>

Excellent! @robliou01I am going to be adding a beta label to that code sample as it will be changing. I have been iterating over it and we found a “bug” in React Router `v6` that causes unnecessary re-renders when one uses `useNavigate()`.

Please refer to the `basic-authentication-bug` branch of the TypeScript version to see the latest integration strategy with React Router `v6`:

[https://github.com/auth0-developer-hub/spa\_react\_typescript\_hello-world\_react-router-6/tree/basic-authentication-bug](https://github.com/auth0-developer-hub/spa_react_typescript_hello-world_react-router-6/tree/basic-authentication-bug)

More than likely, I will be using this approach based on `BrowserRouter` in both the TypeScript and JavaScript code sample. Please if you have any question, do not hesitate to let me know!

---

<div class="post-metadata">

**Author:** ![dean2](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dean2/32/35668_2.png) [@dean2](https://community.auth0.com/u/dean2)\
**Post date:** [March 8, 2022, 3:30am UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/17 "2022-03-08T03:30:52Z")

</div>

> [@dan-auth0](#):
>
> de sample, you need to be logged in in order to access the profile page, `/profile` . Is that different from

First, thankyou and awesome work @dan-auth0 !!! I noticed that one can do Routes-\>Route-\>Routes-\>Route.

This got me thinking, perhaps instead “is there a way to do ProtectedRoutes plural?”

I only ask as the v5 solution was so readable and I realize you are working with v6 and have limitations. Is there a way to override Routes instead of Route and create a cleaner solutions with ProtectedRoutes and then a list of Route that are protected. This overall could be a cleaner solution. It is quite annoying that router v6 is creating this ugly hard to read mess now albeit it cleaned up other things (I guess as developers we are always posed with tradeoffs)

at any rate, thanks for all that work either way. CAVEAT: I am quite a newb in react/typescript so take everything above with the smallest grain of salt possible.

---

<div class="post-metadata">

**Author:** ![dan-auth0](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@dan-auth0](https://community.auth0.com/u/dan-auth0)\
**Post date:** [March 10, 2022, 1:34pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/18 "2022-03-10T13:34:09Z")

</div>

You are welcome, Dean. I’ll share your feedback with the SDK team. I think for now, it’s a bit clearer to implement the guard component per route. I think that using the single-route component makes the process more explicit and intentional, which can mitigate errors such as leaving a route component out of that protected routes bundle by accident.

---

<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:** [March 25, 2022, 1:34pm UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/19 "2022-03-25T13:34:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dan-auth0](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@dan-auth0](https://community.auth0.com/u/dan-auth0)\
**Post date:** [January 6, 2023, 1:22am UTC](https://community.auth0.com/t/implementing-auth0-protected-routes-using-react-router-v6/75667/20 "2023-01-06T01:22:38Z")

</div>

Please consult our newly released [React Authentication By Example: Using React Router 6](https://developer.auth0.com/resources/guides/spa/react/basic-authentication) 🎉 for guidance on how to integrate Auth0 with a React Router `v6` application. While the guide uses React `v18`, the same architecture would apply to an app that uses React `v17`.
