diff --git a/EXAMPLES.md b/EXAMPLES.md index fcefdc28..a2d48dea 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -11,6 +11,7 @@ - [Device-bound tokens with DPoP](#device-bound-tokens-with-dpop) - [Using Multi Resource Refresh Tokens](#using-multi-resource-refresh-tokens) - [Connect Accounts for using Token Vault](#connect-accounts-for-using-token-vault) +- [Access Auth0 Configuration](#access-auth0-configuration) ## Use with a Class Component @@ -98,7 +99,6 @@ const Posts = () => { export default Posts; ``` - ## Custom token exchange Exchange an external subject token for Auth0 tokens using the token exchange flow (RFC 8693): @@ -737,4 +737,27 @@ When the redirect completes, the user will be returned to the application and th ``` -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. \ No newline at end of file +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. + +## Access Auth0 Configuration + +Access the Auth0 domain and client ID that were configured in the `Auth0Provider`: + +```jsx +import React from 'react'; +import { useAuth0 } from '@auth0/auth0-react'; + +const MyComponent = () => { + const { getDomain, getClientId } = useAuth0(); + + const domain = getDomain(); + const clientId = getClientId(); + + // Use domain and clientId as needed + // ... + + return
{/* Your component */}
; +}; + +export default MyComponent; +``` \ No newline at end of file diff --git a/__tests__/auth-provider.test.tsx b/__tests__/auth-provider.test.tsx index 2a3685da..794e9da5 100644 --- a/__tests__/auth-provider.test.tsx +++ b/__tests__/auth-provider.test.tsx @@ -134,6 +134,21 @@ describe('Auth0Provider', () => { }); }); + it('should expose getDomain and getClientId methods', async () => { + const opts = { + clientId: 'test-client-id', + domain: 'test-domain.auth0.com', + }; + const wrapper = createWrapper(opts); + const { result } = renderHook(() => useAuth0(), { + wrapper, + }); + await waitFor(() => { + expect(result.current.getDomain()).toBe('test-domain.auth0.com'); + expect(result.current.getClientId()).toBe('test-client-id'); + }); + }); + it('should check session when logged out', async () => { const wrapper = createWrapper(); const { result } = renderHook( diff --git a/src/auth0-context.tsx b/src/auth0-context.tsx index ba3407cc..57493113 100644 --- a/src/auth0-context.tsx +++ b/src/auth0-context.tsx @@ -241,6 +241,21 @@ export interface Auth0ContextInterface * Check the `EXAMPLES.md` file for a deeper look into this method. */ createFetcher: Auth0Client['createFetcher']; + + /** + * + * Returns the Auth0 domain configured in the Auth0Provider. + * **Use Cases:** + * - Advanced integrations requiring the tenant domain + * + * @returns The Auth0 domain (e.g., "tenant.auth0.com") + */ + getDomain: () => string; + + /** + * Returns the Auth0 client ID that was configured in the Auth0Provider. + */ + getClientId: () => string; } /** @@ -270,6 +285,8 @@ export const initialContext = { setDpopNonce: stub, generateDpopProof: stub, createFetcher: stub, + getDomain: stub, + getClientId: stub, }; /** diff --git a/src/auth0-provider.tsx b/src/auth0-provider.tsx index 54eb5451..dea44d74 100644 --- a/src/auth0-provider.tsx +++ b/src/auth0-provider.tsx @@ -159,6 +159,10 @@ const Auth0Provider = (opts: Auth0ProviderOptions, initialAuthState as AuthState); const didInitialise = useRef(false); + // Store domain and clientId for later access + const domainRef = useRef(clientOpts.domain); + const clientIdRef = useRef(clientOpts.clientId); + const handleError = useCallback((error: Error) => { dispatch({ type: 'ERROR', error }); return error; @@ -341,6 +345,10 @@ const Auth0Provider = (opts: Auth0ProviderOptions domainRef.current, []); + + const getClientId = useCallback(() => clientIdRef.current, []); + const contextValue = useMemo>(() => { return { ...state, @@ -357,6 +365,8 @@ const Auth0Provider = (opts: Auth0ProviderOptions(opts: Auth0ProviderOptions{children};