Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -737,4 +737,27 @@ When the redirect completes, the user will be returned to the application and th
</Auth0Provider>
```

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.
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 <div>{/* Your component */}</div>;
};

export default MyComponent;
```
15 changes: 15 additions & 0 deletions __tests__/auth-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 17 additions & 0 deletions src/auth0-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ export interface Auth0ContextInterface<TUser extends User = User>
* 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;
}

/**
Expand Down Expand Up @@ -270,6 +285,8 @@ export const initialContext = {
setDpopNonce: stub,
generateDpopProof: stub,
createFetcher: stub,
getDomain: stub,
getClientId: stub,
};

/**
Expand Down
12 changes: 12 additions & 0 deletions src/auth0-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
const [state, dispatch] = useReducer(reducer<TUser>, initialAuthState as AuthState<TUser>);
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;
Expand Down Expand Up @@ -341,6 +345,10 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
[client]
);

const getDomain = useCallback(() => domainRef.current, []);

const getClientId = useCallback(() => clientIdRef.current, []);

const contextValue = useMemo<Auth0ContextInterface<TUser>>(() => {
return {
...state,
Expand All @@ -357,6 +365,8 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
setDpopNonce,
generateDpopProof,
createFetcher,
getDomain,
getClientId,
};
}, [
state,
Expand All @@ -373,6 +383,8 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
setDpopNonce,
generateDpopProof,
createFetcher,
getDomain,
getClientId,
]);

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