# Complete Auth0 example for react router v6?

**URL:** <https://community.auth0.com/t/complete-auth0-example-for-react-router-v6/112173>\
**Category:** Get Help\
**Tags:** sdks-quickstarts, auth0-react\
**Created:** [July 26, 2023, 4:25pm UTC](https://community.auth0.com/t/complete-auth0-example-for-react-router-v6/112173 "2023-07-26T16:25:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![neilenns](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/neilenns/32/49294_2.png) [@neilenns](https://community.auth0.com/u/neilenns)\
**Post date:** [July 26, 2023, 4:25pm UTC](https://community.auth0.com/t/complete-auth0-example-for-react-router-v6/112173/1 "2023-07-26T16:25:27Z")

</div>

Does anyone have a complete, working, exampe of using Auth0 with react router v6? Specifically:

1. How to create an Auth0 component that can make use of the react router v6 navigation model
2. How to wrap routes created with createBrowserRouter() with the Auth0 component
3. How to protect those routes
4. How to call protected APIs from v6 loaders and actions that need an Auth0 token

I’ve managed to get something that seems to work for items 1-3 based on snippets from random Stack Overflow questions, but have failed completely at the 4th. It would be fantastic to have an updated SDK sample that makes use of router v6 instead of the current one that is now sadly out of date.

---

<div class="post-metadata">

**Author:** ![dawid.matuszczyk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dawid.matuszczyk/32/74550_2.png) [@dawid.matuszczyk](https://community.auth0.com/u/dawid.matuszczyk)\
**Post date:** [July 31, 2023, 8:55pm UTC](https://community.auth0.com/t/complete-auth0-example-for-react-router-v6/112173/2 "2023-07-31T20:55:55Z")

</div>

Hi @neilenns

Welcome to the Auth0 Community!

Did you have a chance to check our guide → [React Authentication By Example:Using React Router 6](https://developer.auth0.com/resources/guides/spa/react/basic-authentication#call-a-protected-api-from-react) with a code snippet that can be found [here](https://github.com/auth0-developer-hub/spa_react_javascript_hello-world/tree/basic-authentication).

If you find this guide/github repo outdated, please open an issue, and I will ping the repo maintainer about this.

I hope it will help you

Thanks, and have a great day!  
Dawid

---

<div class="post-metadata">

**Author:** ![neilenns](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/neilenns/32/49294_2.png) [@neilenns](https://community.auth0.com/u/neilenns)\
**Post date:** [August 1, 2023, 1:18pm UTC](https://community.auth0.com/t/complete-auth0-example-for-react-router-v6/112173/3 "2023-08-01T13:18:39Z")

</div>

Thanks for the reply!

I did and unfortunately it does not show all the steps required to use Auth0 with a react-router v6 app:

1. The AuthenticationProvider example still uses Routes elements in the demo. With v6 the updated way to create routes is with createRouterBrowser and there’s extra work required to use v6’s navigation (see this [stackoverflow question/answer](https://stackoverflow.com/questions/73934043/how-to-use-auth0-provider-with-the-new-createbrowsererouter-in-v6-4)).
2. It doesn’t show how to access Auth0 tokens in a [loader](https://reactrouter.com/en/main/route/loader) for retrieving data (or [action](https://reactrouter.com/en/main/route/action) for sending data, such as logging in or posting data to a protected API).
3. (Not directly related to react-router) it doesn’t show TypeScript types in the examples., which would be a nice addition.

By far the biggest problem is #2. The v6 model is to use loaders and actions for data and I was unable to get them working with Auth0. I figured out how to pass the Auth0 token function to an action (see [this StackOverflow answer](https://stackoverflow.com/a/76767237/9206264)) but it always fails saying the context wasn’t created yet.

---

<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:** [August 15, 2023, 1:19pm UTC](https://community.auth0.com/t/complete-auth0-example-for-react-router-v6/112173/4 "2023-08-15T13:19:23Z")

</div>

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