Skip to content

Commit c25cb38

Browse files
feat: add getConfiguration method to Auth0Client and updated documentation (#991)
## Summary Adds a `getConfiguration()` method to the auth0-react SDK that exposes the Auth0 domain and clientId configuration values. This method delegates to the underlying `Auth0Client.getConfiguration()` from `@auth0/auth0-spa-js`. Developers need programmatic access to the SDK configuration they provided during initialization. Without this, developers must manually track domain and clientId values separately, leading to: - Duplication of configuration values - Potential inconsistencies - Extra boilerplate for building custom Auth0-related URLs ## Changes ### New Feature - **`getConfiguration()`** - Returns a readonly object containing `{ domain, clientId }` from the SDK initialization ## Usage ### With useAuth0 hook ```tsx import { useAuth0 } from '@auth0/auth0-react'; const MyComponent = () => { const { getConfiguration } = useAuth0(); const config = getConfiguration(); console.log(config.domain); // 'tenant.auth0.com' console.log(config.clientId); // 'abc123' }; ``` ### With withAuth0 HOC ```tsx import { withAuth0, WithAuth0Props } from '@auth0/auth0-react'; class MyComponent extends Component<WithAuth0Props> { render() { const config = this.props.auth0.getConfiguration(); return <div>Domain: {config.domain}</div>; } } export default withAuth0(MyComponent); ``` ## Test Plan - [x] Existing tests pass (102 tests) - [x] 100% code coverage maintained - [x] New relevant tests added: - [x] Lint passes - [x] Build succeeds ## Breaking Changes None. This is a purely additive change.
1 parent 1779641 commit c25cb38

File tree

8 files changed

+127
-9
lines changed

8 files changed

+127
-9
lines changed

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
});

package-lock.json

Lines changed: 50 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@
9595
"react-dom": "^16.11.0 || ^17 || ^18 || ~19.0.1 || ~19.1.2 || ^19.2.1"
9696
},
9797
"dependencies": {
98-
"@auth0/auth0-spa-js": "^2.11.0"
98+
"@auth0/auth0-spa-js": "^2.12.0"
9999
}
100100
}

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)