-
Notifications
You must be signed in to change notification settings - Fork 290
feat: add client prop to Auth0Provider #1041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
946c915
4f96be5
c6bc6a2
82aa2a3
bef507a
615515a
81f8108
7d33191
4889dce
7207306
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| - [Use with a Class Component](#use-with-a-class-component) | ||
| - [Protect a Route](#protect-a-route) | ||
| - [Call an API](#call-an-api) | ||
| - [Use Auth0 outside of React](#use-auth0-outside-of-react) | ||
| - [Protecting a route in a `react-router-dom v6` app](#protecting-a-route-in-a-react-router-dom-v6-app) | ||
| - [Protecting a route in a Gatsby app](#protecting-a-route-in-a-gatsby-app) | ||
| - [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 = () => { | |
| export default Posts; | ||
| ``` | ||
|
|
||
| ## Use Auth0 outside of React | ||
|
|
||
| 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. | ||
|
|
||
| ```jsx | ||
| // auth0-client.js | ||
| import { Auth0Client } from '@auth0/auth0-spa-js'; | ||
|
|
||
| export const auth0Client = new Auth0Client({ | ||
| domain: 'YOUR_AUTH0_DOMAIN', | ||
| clientId: 'YOUR_AUTH0_CLIENT_ID', | ||
| authorizationParams: { | ||
| redirect_uri: window.location.origin, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Pass the client to `Auth0Provider`: | ||
|
|
||
| ```jsx | ||
| import { Auth0Provider } from '@auth0/auth0-react'; | ||
| import { auth0Client } from './auth0-client'; | ||
|
|
||
| export default function App() { | ||
| return ( | ||
| <Auth0Provider client={auth0Client}> | ||
| <MyApp /> | ||
| </Auth0Provider> | ||
| ); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should highlight potential pitfalls and anti patterns with this approach. What happens if a user initilaise export const auth0Client = createAuth0Client({
domain: 'YOUR_AUTH0_DOMAIN',
clientId: 'YOUR_AUTH0_CLIENT_ID',
authorizationParams: {
redirect_uri: window.location.origin,
},
}); <Auth0Provider
client={auth0Client}
domain={config.domain}
clientId={config.clientId}
key={JSON.stringify(config)}
authorizationParams={{
redirect_uri: window.location.origin,
audience: config.audience || 'default'
}}
...
>
</Auth0Provider>I believe, currently this is allowed, should we add checks to restrict this behaviour or at-least document this as an anti pattern.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Added a runtime |
||
| ``` | ||
|
|
||
| > **Note:** | ||
| > - The raw `Auth0Client` method is `getTokenSilently()`, not `getAccessTokenSilently()`. They share the same token cache but the hook version also updates React state. | ||
| > - 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. | ||
|
|
||
| Use the same client instance in a TanStack Start client function middleware: | ||
|
|
||
| ```js | ||
| import { createMiddleware } from '@tanstack/react-start'; | ||
| import { auth0Client } from './auth0-client'; | ||
|
|
||
| export const authMiddleware = createMiddleware({ type: 'function' }).client( | ||
| async ({ next }) => { | ||
| const token = await auth0Client.getTokenSilently(); | ||
| return next({ | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| }, | ||
| }); | ||
| }, | ||
| ); | ||
| ``` | ||
|
|
||
| ## Custom token exchange | ||
|
|
||
| Exchange an external subject token for Auth0 tokens using the token exchange flow (RFC 8693): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may also lead to inconsistent api behaviour, for example
Someone who creates a rawClient would need to call
vs
Call silent token token via auth0 hook
Should we highlight this potential mismatch.
Additionally for anyone using the raw auth0 client via
createAuth0Client(...)will internal state in react sdk created in https://github.com/auth0/auth0-react/blob/main/src/reducer.tsx will be updated ?In case they are not, will there be potential side effects ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a note on both points: the method naming difference (
getTokenSilentlyvsgetAccessTokenSilently), the state sync behaviour, and a callout to avoid callingclient.logout()directly on the raw client.