Skip to content

Commit bd693e2

Browse files
feat: add getConfiguration method to Auth0Client and updated documentation
1 parent a57cb5e commit bd693e2

6 files changed

Lines changed: 76 additions & 3 deletions

File tree

EXAMPLES.md

Lines changed: 28 additions & 1 deletion
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 SDK Configuration](#access-sdk-configuration)
1415

1516
## Use with a Class Component
1617

@@ -737,4 +738,30 @@ When the redirect completes, the user will be returned to the application and th
737738
</Auth0Provider>
738739
```
739740
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+
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.
742+
743+
## Access SDK Configuration
744+
745+
Retrieve the Auth0 domain and client ID that were used to configure the SDK:
746+
747+
```jsx
748+
import React from 'react';
749+
import { useAuth0 } from '@auth0/auth0-react';
750+
751+
const ConfigInfo = () => {
752+
const { getConfiguration } = useAuth0();
753+
754+
const config = getConfiguration();
755+
756+
return (
757+
<div>
758+
<p>Domain: {config.domain}</p>
759+
<p>Client ID: {config.clientId}</p>
760+
</div>
761+
);
762+
};
763+
764+
export default ConfigInfo;
765+
```
766+
767+
This is useful for debugging, logging, or building custom Auth0-related URLs without duplicating configuration values.

__mocks__/@auth0/auth0-spa-js.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const getDpopNonce = jest.fn();
1818
const setDpopNonce = jest.fn();
1919
const generateDpopProof = jest.fn();
2020
const createFetcher = jest.fn();
21+
const getConfiguration = jest.fn();
2122

2223
export const Auth0Client = jest.fn(() => {
2324
return {
@@ -39,6 +40,7 @@ export const Auth0Client = jest.fn(() => {
3940
setDpopNonce,
4041
generateDpopProof,
4142
createFetcher,
43+
getConfiguration,
4244
};
4345
});
4446

__tests__/auth-provider.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,4 +1257,28 @@ describe('Auth0Provider', () => {
12571257
expect(screen.queryByText('__custom_user__')).toBeInTheDocument();
12581258
expect(screen.queryByText('__main_user__')).not.toBeInTheDocument();
12591259
});
1260+
1261+
describe('getConfiguration', () => {
1262+
it('should return configuration from Auth0Client', async () => {
1263+
clientMock.getConfiguration.mockReturnValue({
1264+
domain: 'test.auth0.com',
1265+
clientId: 'test-client-id'
1266+
});
1267+
1268+
const wrapper = createWrapper();
1269+
const { result } = renderHook(() => useContext(Auth0Context), { wrapper });
1270+
1271+
await waitFor(() => {
1272+
expect(result.current.isLoading).toBe(false);
1273+
});
1274+
1275+
const config = result.current.getConfiguration();
1276+
1277+
expect(clientMock.getConfiguration).toHaveBeenCalled();
1278+
expect(config).toEqual({
1279+
domain: 'test.auth0.com',
1280+
clientId: 'test-client-id'
1281+
});
1282+
});
1283+
});
12601284
});

src/auth0-context.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,21 @@ export interface Auth0ContextInterface<TUser extends User = User>
237237
* This is a drop-in replacement for the Fetch API's `fetch()` method, but will
238238
* handle certain authentication logic for you, like building the proper auth
239239
* headers or managing DPoP nonces and retries automatically.
240-
*
240+
*
241241
* Check the `EXAMPLES.md` file for a deeper look into this method.
242242
*/
243243
createFetcher: Auth0Client['createFetcher'];
244+
245+
/**
246+
* ```js
247+
* const config = getConfiguration();
248+
* // { domain: 'tenant.auth0.com', clientId: 'abc123' }
249+
* ```
250+
*
251+
* Returns a readonly copy of the initialization configuration
252+
* containing the domain and clientId.
253+
*/
254+
getConfiguration: Auth0Client['getConfiguration'];
244255
}
245256

246257
/**
@@ -270,6 +281,7 @@ export const initialContext = {
270281
setDpopNonce: stub,
271282
generateDpopProof: stub,
272283
createFetcher: stub,
284+
getConfiguration: stub,
273285
};
274286

275287
/**

src/auth0-provider.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
341341
[client]
342342
);
343343

344+
const getConfiguration = useCallback<Auth0Client['getConfiguration']>(
345+
() => client.getConfiguration(),
346+
[client]
347+
);
348+
344349
const contextValue = useMemo<Auth0ContextInterface<TUser>>(() => {
345350
return {
346351
...state,
@@ -357,6 +362,7 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
357362
setDpopNonce,
358363
generateDpopProof,
359364
createFetcher,
365+
getConfiguration,
360366
};
361367
}, [
362368
state,
@@ -373,6 +379,7 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
373379
setDpopNonce,
374380
generateDpopProof,
375381
createFetcher,
382+
getConfiguration,
376383
]);
377384

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

src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export {
4545
ResponseType,
4646
ConnectError,
4747
CustomTokenExchangeOptions,
48-
TokenEndpointResponse
48+
TokenEndpointResponse,
49+
ClientConfiguration,
4950
} from '@auth0/auth0-spa-js';
5051
export { OAuthError } from './errors';

0 commit comments

Comments
 (0)