Corporate proxy support
Route provider requests through an organization’s HTTP proxy.
Auth.js · Function
customFetch lets an OAuth provider use a replacement fetch function for direct requests to its OAuth endpoints. It supports integrations such as proxies, custom fetch libraries, logging, mocks, and provider-specific request adjustments.
customFetch is a symbol-based provider option that replaces the default fetch used for direct OAuth-provider endpoint requests. Assign it a function compatible with fetch; the function receives the request input and optional initialization data and must return a Promise resolving to a Response.
const customFetch: typeof customFetch;| Name | Type | Description |
|---|---|---|
input | URL | RequestInfo | Request URL or request object passed to fetch. |
init | RequestInit | Optional Fetch API request initialization options. |
Promise<Response>
import { Auth, customFetch } from "@auth/core"
import GitHub from "@auth/core/providers/github"
import { ProxyAgent, fetch as undici } from "undici"
const dispatcher = new ProxyAgent("my.proxy.server")
function proxy(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {
return undici(args[0], { ...(args[1] ?? {}), dispatcher })
}
const response = await Auth(request, {
providers: [GitHub({ [customFetch]: proxy })]
})Overrides the provider's OAuth HTTP requests with a fetch implementation that routes through an Undici proxy agent.
Route provider requests through an organization’s HTTP proxy.
Use a compatible fetch implementation to alter transport behavior.
Replace network calls with controlled responses during automated tests.
Add diagnostics or provider-specific request metadata.
customFetch is an exported symbol used as a computed provider option. Setting it to a fetch-compatible function makes that provider use the replacement for requests to its OAuth endpoints.
Follow-up: How would you apply it to only one provider? Pass the computed property on that provider's configuration, for example GitHub({ [customFetch]: proxy }).
The function accepts the same input and optional initialization arguments as fetch and returns Promise<Response>. This lets the implementation add transport behavior while preserving the expected Web Fetch contract.
Follow-up: What types should a proxy wrapper preserve? Use Parameters<typeof fetch> for its arguments and ReturnType<typeof fetch> for its result, as in the documented example.
Typical uses include corporate proxies, alternate fetch libraries, endpoint-cache discovery, test mocks, logging, and adding headers or parameters for providers that do not follow the specification. The override targets direct OAuth endpoint calls.
Follow-up: What should be checked before deploying a custom transport? Verify its security behavior and runtime compatibility, especially when using proxy-specific libraries.
AuthGitHubfetchProxyAgent