Skip to content

Commit 3ef9f0a

Browse files
Merge pull request #256 from auth0/feat/custom-fetcher-proxy-mode
feat(core,react): add custom fetcher support for proxy mode
2 parents a29ae8e + 61e379a commit 3ef9f0a

8 files changed

Lines changed: 25 additions & 7 deletions

File tree

packages/core/src/api/api-utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ export const AUTH0_SCOPE_HEADER = HeaderName.Auth0Scope;
1313
/**
1414
* Creates a fetcher function for proxy mode that injects scopes via auth0-scope header.
1515
* The proxy will extract scopes from the header and request the appropriate token.
16+
* @param customFetcher - Optional custom fetch implementation to use instead of global fetch
1617
* @returns Fetcher function that sets auth0-scope and content-type headers
1718
* @internal
1819
*/
19-
export function createProxyFetcher(): FetcherSupplier {
20+
export function createProxyFetcher(
21+
customFetcher?: (url: string, init?: RequestInit) => Promise<Response>,
22+
): FetcherSupplier {
23+
const fetchFn = customFetcher ?? fetch;
2024
return async (url, init, authParams) => {
2125
const headers = new Headers(init?.headers);
2226
headers.set(HeaderName.ContentType, ContentType.JSON);
2327
if (authParams?.scope?.length) {
2428
headers.set(HeaderName.Auth0Scope, authParams.scope.join(' '));
2529
}
26-
return fetch(url, { ...init, headers });
30+
return fetchFn(url, { ...init, headers });
2731
};
2832
}
2933

packages/core/src/auth/auth-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export type ProxyAuthConfig = {
101101
mode: 'proxy';
102102
proxyUrl: string;
103103
domain?: string;
104+
fetcher?: (url: string, init?: RequestInit) => Promise<Response>;
104105
};
105106

106107
/**
@@ -127,6 +128,7 @@ export interface AuthDetails {
127128
domain?: string | undefined;
128129
authProxyUrl?: string | undefined;
129130
contextInterface?: BasicAuth0ContextInterface | undefined;
131+
fetcher?: (url: string, init?: RequestInit) => Promise<Response>;
130132
previewMode?: boolean; // For docs - skip API client initialization
131133
}
132134

packages/core/src/auth/auth-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const AuthUtils = {
5353
mode: 'proxy',
5454
proxyUrl: normalizeProxyUrl(auth.authProxyUrl),
5555
...(auth.domain && { domain: auth.domain.trim() }),
56+
...(auth.fetcher && { fetcher: auth.fetcher }),
5657
};
5758
}
5859

packages/core/src/services/my-account/my-account-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function createMyAccountClient(config: ClientAuthConfig) {
2424
domain: '',
2525
baseUrl: new URL(MY_ACCOUNT_PROXY_PATH, config.proxyUrl).href,
2626
telemetry: false,
27-
fetcher: createProxyFetcher(),
27+
fetcher: createProxyFetcher(config.fetcher),
2828
});
2929
}
3030

packages/core/src/services/my-organization/my-organization-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function createMyOrganizationClient(config: ClientAuthConfig) {
2424
domain: '',
2525
baseUrl: new URL(MY_ORGANIZATION_PROXY_PATH, config.proxyUrl).href,
2626
telemetry: false,
27-
fetcher: createProxyFetcher(),
27+
fetcher: createProxyFetcher(config.fetcher),
2828
});
2929
}
3030

packages/react/src/providers/proxy-provider.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ export const Auth0ComponentProvider = ({
4343
children,
4444
}: Extract<Auth0ComponentProviderProps, { mode: 'proxy' }> & { children: React.ReactNode }) => {
4545
const mergedToastSettings = useToastProvider(toastSettings);
46-
const baseUrl = proxyConfig.baseUrl;
46+
const { baseUrl, fetcher } = proxyConfig;
4747

4848
const memoizedAuthDetails = React.useMemo<AuthDetails>(
4949
() => ({
5050
domain,
5151
authProxyUrl: baseUrl,
52+
fetcher,
5253
previewMode,
5354
}),
54-
[domain, baseUrl, previewMode],
55+
[domain, baseUrl, fetcher, previewMode],
5556
);
5657

5758
const coreClient = useCoreClientInitialization({

packages/react/src/types/auth-types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ export type Auth0ComponentProviderProps = (
1818
authContext?: AuthDetails['contextInterface'];
1919
proxyConfig?: never;
2020
}
21-
| { mode: 'proxy'; domain: string; proxyConfig: { baseUrl: string } }
21+
| {
22+
mode: 'proxy';
23+
domain: string;
24+
proxyConfig: {
25+
baseUrl: string;
26+
fetcher?: (url: string, init?: RequestInit) => Promise<Response>;
27+
};
28+
}
2229
) & {
2330
i18n?: I18nOptions;
2431
themeSettings?: ThemeSettings;

packages/react/src/types/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* @module types
44
*/
55

6+
// Auth types
7+
export * from './auth-types';
8+
69
// My Account types
710
export * from './my-account/mfa/mfa-types';
811

0 commit comments

Comments
 (0)