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.
Using Undici Library
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 })], })Using HttpsProxyAgent
On Edge Runtimes or with proxy restrictions, the undici library may not work. Using a simpler approach with HttpsProxyAgent by passing a proxyAgent to the fetch implementation.
auth.ts
import NextAuth, { customFetch } from "next-auth" import GitHub from "next-auth/providers/github" const { HttpsProxyAgent } = require("https-proxy-agent") const proxyAgent = new HttpsProxyAgent("my.proxy.server") async function proxy(url: string, options: any): Promise<Response> { const response = (await fetch(url, { ...options, agent: proxyAgent, })) as unknown as Response return response } export const { handlers, auth } = NextAuth({ providers: [GitHub({ [customFetch]: proxy })], })