You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## What
Adds a `client` prop to `Auth0Provider` so a pre-configured
`Auth0Client` instance can be shared between the React tree and code
outside of React's lifecycle (e.g. TanStack Start client function
middleware).
Closes#1037. Supersedes #849 (thanks to @rodrigowbazevedo for the
original draft).
## Usage
```tsx
import { Auth0Client } from '@auth0/auth0-spa-js';
import { Auth0Provider } from '@auth0/auth0-react';
const client = new Auth0Client({ domain, clientId });
// Outside React (e.g. TanStack Start client function middleware)
// Note: the raw Auth0Client method is getTokenSilently (not getAccessTokenSilently)
const token = await client.getTokenSilently();
// Inside React — Auth0Provider uses the same client instance
function App() {
return <Auth0Provider client={client}><MyApp /></Auth0Provider>;
}
```
## Notes
- Fully backward compatible — existing usage without `client` prop is
unchanged
- Calling methods on the raw client does not update React state — use
hooks inside React for that
Copy file name to clipboardExpand all lines: EXAMPLES.md
+55Lines changed: 55 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@
3
3
-[Use with a Class Component](#use-with-a-class-component)
4
4
-[Protect a Route](#protect-a-route)
5
5
-[Call an API](#call-an-api)
6
+
-[Use Auth0 outside of React](#use-auth0-outside-of-react)
6
7
-[Protecting a route in a `react-router-dom v6` app](#protecting-a-route-in-a-react-router-dom-v6-app)
7
8
-[Protecting a route in a Gatsby app](#protecting-a-route-in-a-gatsby-app)
8
9
-[Protecting a route in a Next.js app (in SPA mode)](#protecting-a-route-in-a-nextjs-app-in-spa-mode)
@@ -102,6 +103,60 @@ const Posts = () => {
102
103
exportdefaultPosts;
103
104
```
104
105
106
+
## Use Auth0 outside of React
107
+
108
+
If you need to share an `Auth0Client` instance between the React tree and code that has no access to React's lifecycle — such as TanStack Start client function middleware — create an `Auth0Client` and pass it to `Auth0Provider` via the `client` prop.
> - The raw `Auth0Client` method is `getTokenSilently()`, not `getAccessTokenSilently()`. They share the same token cache but the hook version also updates React state.
140
+
> - Calling methods on the raw client does not update React state. For token fetching this is fine since the cache is shared. Avoid calling `client.logout()` directly — use the `logout` method from `useAuth0` instead so React state stays in sync.
141
+
142
+
Use the same client instance in a TanStack Start client function middleware:
* For a sample on using multiple Auth0Providers review the [React Account Linking Sample](https://github.com/auth0-samples/auth0-link-accounts-sample/tree/react-variant)
0 commit comments