跳至內容
正從 NextAuth.js v4 遷移?請閱讀 我們的遷移指南.
指南支援企業代理

支援企業代理

Auth.js 函式庫使用 fetch API 與 OAuth 供應商通訊。如果您的組織使用企業代理,您可能需要設定 fetch API 以使用代理。

使用自訂 fetch 函數

您可以透過將自訂 fetch 函數作為選項傳遞給供應商。

在這裡,我們使用 undici 函式庫透過代理伺服器發出請求,方法是將 dispatcher 傳遞給 undicifetch 實作。

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 })],
})

資源

Auth.js © Balázs Orbán 與團隊 -2024