@@ -51,10 +51,7 @@ export type AppState = {
5151 [ key : string ] : any ; // eslint-disable-line @typescript-eslint/no-explicit-any
5252} ;
5353
54- /**
55- * The main configuration to instantiate the `Auth0Provider`.
56- */
57- export interface Auth0ProviderOptions < TUser extends User = User > extends Auth0ClientOptions {
54+ type Auth0ProviderBaseOptions < TUser extends User = User > = {
5855 /**
5956 * The child nodes your Provider has wrapped
6057 */
@@ -97,7 +94,31 @@ export interface Auth0ProviderOptions<TUser extends User = User> extends Auth0Cl
9794 * 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)
9895 */
9996 context ?: React . Context < Auth0ContextInterface < TUser > > ;
100- }
97+ } ;
98+
99+ /**
100+ * Options for `Auth0Provider` when configuring Auth0 via `domain` and `clientId`.
101+ * Use this type when building wrapper components around `Auth0Provider`.
102+ */
103+ export type Auth0ProviderWithConfigOptions < TUser extends User = User > =
104+ Auth0ProviderBaseOptions < TUser > & Auth0ClientOptions & { client ?: never } ;
105+
106+ /**
107+ * Options for `Auth0Provider` when supplying a pre-configured `Auth0Client` instance.
108+ * Use `createAuth0Client` to create the client so telemetry is set correctly.
109+ */
110+ export type Auth0ProviderWithClientOptions < TUser extends User = User > =
111+ Auth0ProviderBaseOptions < TUser > & Partial < Auth0ClientOptions > & { client : Auth0Client } ;
112+
113+ /**
114+ * The main configuration to instantiate the `Auth0Provider`.
115+ *
116+ * Either provide `domain` and `clientId` (`Auth0ProviderWithConfigOptions`)
117+ * or a pre-configured `client` instance (`Auth0ProviderWithClientOptions`).
118+ */
119+ export type Auth0ProviderOptions < TUser extends User = User > =
120+ | Auth0ProviderWithConfigOptions < TUser >
121+ | Auth0ProviderWithClientOptions < TUser > ;
101122
102123/**
103124 * Replaced by the package version at build time.
@@ -109,7 +130,7 @@ declare const __VERSION__: string;
109130 * @ignore
110131 */
111132const toAuth0ClientOptions = (
112- opts : Auth0ProviderOptions
133+ opts : Auth0ClientOptions
113134) : Auth0ClientOptions => {
114135 deprecateRedirectUri ( opts ) ;
115136
@@ -122,6 +143,35 @@ const toAuth0ClientOptions = (
122143 } ;
123144} ;
124145
146+ /**
147+ * Creates a new `Auth0Client` with the `auth0-react` SDK telemetry header set.
148+ *
149+ * Use this when you need to share a single client instance with `Auth0Provider`
150+ * and also access Auth0 outside of React (e.g. in middleware or interceptors).
151+ *
152+ * @example
153+ * ```tsx
154+ * const client = createAuth0Client({ domain, clientId });
155+ *
156+ * // Use outside React (e.g. in TanStack middleware, axios interceptors, Redux middleware)
157+ * const token = await client.getTokenSilently();
158+ *
159+ * // Use inside React
160+ * function App() {
161+ * return <Auth0Provider client={client}><MyApp /></Auth0Provider>;
162+ * }
163+ * ```
164+ */
165+ export const createAuth0Client = ( options : Auth0ClientOptions ) : Auth0Client => {
166+ return new Auth0Client ( {
167+ ...options ,
168+ auth0Client : {
169+ name : 'auth0-react' ,
170+ version : __VERSION__ ,
171+ } ,
172+ } ) ;
173+ } ;
174+
125175/**
126176 * @ignore
127177 */
@@ -151,10 +201,11 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
151201 skipRedirectCallback,
152202 onRedirectCallback = defaultOnRedirectCallback ,
153203 context = Auth0Context ,
204+ client : providedClient ,
154205 ...clientOpts
155- } = opts ;
206+ } = opts as Auth0ProviderBaseOptions < TUser > & Auth0ClientOptions & { client ?: Auth0Client } ;
156207 const [ client ] = useState (
157- ( ) => new Auth0Client ( toAuth0ClientOptions ( clientOpts ) )
208+ ( ) => providedClient ?? new Auth0Client ( toAuth0ClientOptions ( clientOpts ) )
158209 ) ;
159210 const [ state , dispatch ] = useReducer ( reducer < TUser > , initialAuthState as AuthState < TUser > ) ;
160211 const didInitialise = useRef ( false ) ;
0 commit comments