跳至內容
從 NextAuth.js v4 遷移?請閱讀 我們的遷移指南.

Azure DevOps 供應商

⚠️

已棄用 - 雖然仍然可用,但 Microsoft 不再支援 Azure DevOps OAuth,並建議改用 Microsoft Entra ID

資源

設定

回調網址

https://example.com/api/auth/callback/azure-devops

環境變數

.env.local 中建立下列項目

AZURE_DEVOPS_APP_ID=<copy App ID value here>
AZURE_DEVOPS_CLIENT_SECRET=<copy generated client secret value here>

註冊應用程式

https://app.vsaex.visualstudio.com/app/register

提供所需的詳細資訊

  1. 公司名稱
  2. 應用程式名稱
  3. 應用程式網站
  4. 授權回調網址
    • 生產環境請使用 https://example.com/api/auth/callback/azure-devops
    • 開發環境請使用 https://127.0.0.1/api/auth/callback/azure-devops
  5. 授權範圍
    • 至少需要 使用者設定檔 (讀取)

點擊「建立應用程式」

⚠️
  • 即使是 localhost,您也必須使用 HTTPS

  • 您必須刪除並建立新的應用程式,才能稍後變更範圍

下列資料與下一步相關

  • 應用程式 ID
  • 用戶端密碼 (在點擊「顯示」按鈕後,忽略上方的應用程式密碼項目)
  • 授權範圍

組態

/auth.ts
import NextAuth from "next-auth"
import AzureDevOps from "next-auth/providers/azure-devops"
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  providers: [
    AzureDevOps({
      clientId: AUTH_AZURE_DEVOPS_APP_ID,
      clientSecret: AUTH_AZURE_DEVOPS_CLIENT_SECRET,
    }),
  ],
})

重新整理 Token 輪替

主要指南作為您的起點,並考慮以下事項

./auth.ts
export const { signIn, signOut, auth } = NextAuth({
  callbacks: {
    async jwt({ token, user, account }) {
      // The token has an absolute expiration time
      const accessTokenExpires = account.expires_at * 1000
    },
  },
})
 
async function refreshAccessToken(token) {
  const response = await fetch(
    "https://app.vssps.visualstudio.com/oauth2/token",
    {
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      method: "POST",
      body: new URLSearchParams({
        client_assertion_type:
          "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
        client_assertion: AZURE_DEVOPS_CLIENT_SECRET,
        grant_type: "refresh_token",
        assertion: token.refreshToken,
        redirect_uri:
          process.env.NEXTAUTH_URL + "/api/auth/callback/azure-devops",
      }),
    }
  )
 
  // The refreshed token comes with a relative expiration time
  const accessTokenExpires = Date.now() + newToken.expires_in * 1000
}
Auth.js © Balázs Orbán 和團隊 -2024