Supporting corporate proxies
Auth.js libraries use the fetch
API to communicate with OAuth providers. If your organization uses a corporate proxy, you may need to configure the fetch
API to use the proxy.
Using a custom fetch function
You can provide a custom fetch
function by passing it as an option to the provider.
Here, we use the undici
library to make requests through a proxy server, by passing a dispatcher
to the fetch
implementation by undici
.
auth.ts
import NextAuth, { customFetch } from "next-auth"
import GitHub from "next-auth/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> {
// @ts-expect-error `undici` has a `duplex` option
return undici(args[0], { ...args[1], dispatcher })
}
export const { handlers, auth } = NextAuth({
providers: [GitHub({ [customFetch]: proxy })],
})