支持企业代理
Auth.js 库使用 fetch API 与 OAuth 提供者通信。如果您的组织使用企业代理,您可能需要配置 fetch API 以使用代理。
使用自定义 fetch 函数
您可以通过将自定义 fetch 函数作为选项传递给提供者来提供它。
这里,我们使用 undici 库通过代理服务器发出请求,方法是将 dispatcher 传递给 undici 实现的 fetch。
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 })],
})