Skip to content

Commit f34c829

Browse files
feat: add methods to access Auth0 domain and client ID in Auth0Provider
1 parent d40b83a commit f34c829

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

EXAMPLES.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [Device-bound tokens with DPoP](#device-bound-tokens-with-dpop)
1212
- [Using Multi Resource Refresh Tokens](#using-multi-resource-refresh-tokens)
1313
- [Connect Accounts for using Token Vault](#connect-accounts-for-using-token-vault)
14+
- [Access Auth0 Configuration](#access-auth0-configuration)
1415

1516
## Use with a Class Component
1617

@@ -97,7 +98,6 @@ const Posts = () => {
9798
};
9899

99100
export default Posts;
100-
```
101101

102102
## Custom token exchange
103103

@@ -737,4 +737,28 @@ When the redirect completes, the user will be returned to the application and th
737737
</Auth0Provider>
738738
```
739739
740-
You can now [call the API](#calling-an-api) with your access token and the API can use [Access Token Exchange with Token Vault](https://auth0.com/docs/secure/tokens/token-vault/access-token-exchange-with-token-vault) to get tokens from the Token Vault to access third party APIs on behalf of the user.
740+
You can now [call the API](#calling-an-api) with your access token and the API can use [Access Token Exchange with Token Vault](https://auth0.com/docs/secure/tokens/token-vault/access-token-exchange-with-token-vault) to get tokens from the Token Vault to access third party APIs on behalf of the user.
741+
742+
## Access Auth0 Configuration
743+
744+
Access the Auth0 domain and client ID that were configured in the `Auth0Provider`. This is useful when building UI components that need access to the configuration without requiring it to be passed as props:
745+
746+
```jsx
747+
import React from 'react';
748+
import { useAuth0 } from '@auth0/auth0-react';
749+
750+
const ConfigDisplay = () => {
751+
const { getDomain, getClientId, isAuthenticated, user } = useAuth0();
752+
753+
return (
754+
<div>
755+
<h3>Auth0 Configuration</h3>
756+
<p>Domain: {getDomain()}</p>
757+
<p>Client ID: {getClientId()}</p>
758+
{isAuthenticated && <p>Logged in as: {user.name}</p>}
759+
</div>
760+
);
761+
};
762+
763+
export default ConfigDisplay;
764+
```

__tests__/auth-provider.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ describe('Auth0Provider', () => {
134134
});
135135
});
136136

137+
it('should expose getDomain and getClientId methods', async () => {
138+
const opts = {
139+
clientId: 'test-client-id',
140+
domain: 'test-domain.auth0.com',
141+
};
142+
const wrapper = createWrapper(opts);
143+
const { result } = renderHook(() => useAuth0(), {
144+
wrapper,
145+
});
146+
await waitFor(() => {
147+
expect(result.current.getDomain()).toBe('test-domain.auth0.com');
148+
expect(result.current.getClientId()).toBe('test-client-id');
149+
});
150+
});
151+
137152
it('should check session when logged out', async () => {
138153
const wrapper = createWrapper();
139154
const { result } = renderHook(

src/auth0-context.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,28 @@ export interface Auth0ContextInterface<TUser extends User = User>
241241
* Check the `EXAMPLES.md` file for a deeper look into this method.
242242
*/
243243
createFetcher: Auth0Client['createFetcher'];
244+
245+
/**
246+
* ```js
247+
* const domain = auth0.getDomain();
248+
* ```
249+
*
250+
* Returns the Auth0 domain that was configured in the Auth0Provider.
251+
* This is useful for UI components that need to access the domain without
252+
* requiring it to be passed as a prop.
253+
*/
254+
getDomain: () => string;
255+
256+
/**
257+
* ```js
258+
* const clientId = auth0.getClientId();
259+
* ```
260+
*
261+
* Returns the Auth0 client ID that was configured in the Auth0Provider.
262+
* This is useful for UI components that need to access the client ID without
263+
* requiring it to be passed as a prop.
264+
*/
265+
getClientId: () => string;
244266
}
245267

246268
/**
@@ -270,6 +292,8 @@ export const initialContext = {
270292
setDpopNonce: stub,
271293
generateDpopProof: stub,
272294
createFetcher: stub,
295+
getDomain: stub,
296+
getClientId: stub,
273297
};
274298

275299
/**

src/auth0-provider.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
159159
const [state, dispatch] = useReducer(reducer<TUser>, initialAuthState as AuthState<TUser>);
160160
const didInitialise = useRef(false);
161161

162+
// Store domain and clientId for later access
163+
const domainRef = useRef(clientOpts.domain);
164+
const clientIdRef = useRef(clientOpts.clientId);
165+
162166
const handleError = useCallback((error: Error) => {
163167
dispatch({ type: 'ERROR', error });
164168
return error;
@@ -341,6 +345,10 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
341345
[client]
342346
);
343347

348+
const getDomain = useCallback(() => domainRef.current, []);
349+
350+
const getClientId = useCallback(() => clientIdRef.current, []);
351+
344352
const contextValue = useMemo<Auth0ContextInterface<TUser>>(() => {
345353
return {
346354
...state,
@@ -357,6 +365,8 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
357365
setDpopNonce,
358366
generateDpopProof,
359367
createFetcher,
368+
getDomain,
369+
getClientId,
360370
};
361371
}, [
362372
state,
@@ -373,6 +383,8 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
373383
setDpopNonce,
374384
generateDpopProof,
375385
createFetcher,
386+
getDomain,
387+
getClientId,
376388
]);
377389

378390
return <context.Provider value={contextValue}>{children}</context.Provider>;

0 commit comments

Comments
 (0)