# 讓使用者在你的網站上使用 Apple 登入

> 使用 AirHost 的登入功能，或讓 Agent 註冊 Services ID、設定網站的網域和返回 URL，並建立伺服器所需的「使用 Apple 登入」金鑰。

Source: https://air.build/tw/docs/build/let-people-sign-in-with-apple-on-your-website/

網站上的「使用 Apple 登入」透過你 Apple 團隊中的 Services ID 運作：它向 Apple 標明你的網站，列出網站所在的網域以及 Apple 讓使用者返回的返回 URL，並與你 App 的 App ID 連結。你網站的伺服器還需要一個「使用 Apple 登入」金鑰。大部分設定由你的 Agent 完成；有一個步驟需要你自己在 Apple 開發者帳號中操作。

> [!NOTE]
> Apple 的[註冊服務 ID](https://developer.apple.com/help/account/identifiers/register-a-services-id/)和[建立私密金鑰以存取服務](https://developer.apple.com/help/account/keys/create-a-private-key/)頁面說明，Services ID 和金鑰需要你在 Apple 開發者團隊中擁有帳號持有人或管理員角色。

> [!NOTE]
> 本頁說明的是你的網站。關於 App 內的「使用 Apple 登入」，請參閱[開啟 iCloud、App Groups 或其他功能](https://air.build/tw/docs/build/turn-on-an-app-capability/)。AirBuild 沒有用於 Services ID 或「使用 Apple 登入」金鑰的畫面：你在專案的聊天中詢問 Agent，它會使用 `asc web service-ids` 和 `asc web sign-in-keys`（請參閱 [asc：網頁工作階段指令](https://air.build/tw/docs/cli/asc-web/#subcommands)）。這些指令使用 AirBuild 中保存的團隊擁有者的 Apple 登入。

## 如果你的網站在 AirHost 上執行

AirHost 是 AirBuild 為你的 App 提供的託管服務，它有自己的登入功能。在你的 App 還沒有登入功能時，**App 使用者** 標籤頁會顯示 **為此 App 加入登入功能**，並附有「開箱即用的 Apple 和 Google 登入」。App 使用它之後，登入的使用者會列在那裡。若要加入，請在該標籤頁上點一下 **請 Agent 接入**，或在聊天中詢問。請參閱[託管 App 的後端、資料庫和登入功能（AirHost）](https://air.build/tw/docs/services/host-a-backend/#ask-your-agents-to-set-it-up)。

本頁其餘部分說明如何在你自己的 Apple 團隊下設定「使用 Apple 登入」，適用於由你自己執行登入的網站。

## 開始之前

- 團隊的擁有者已連結 Apple 開發者帳號並啟用一個團隊。請參閱[連結你的 Apple 開發者帳號](https://air.build/tw/docs/app-store/connect-apple-developer-account/)。
- 你 App 的 App ID 已開啟「使用 Apple 登入」。如果沒有，請詢問「為這個 App 開啟『使用 Apple 登入』。」請參閱[開啟 iCloud、App Groups 或其他功能](https://air.build/tw/docs/build/turn-on-an-app-capability/)。
- 你知道網站的網域（例如 `example.com`）及其返回 URL，也就是使用者登入後 Apple 讓他們返回的位址，例如 `https://example.com/auth/apple/callback`。返回 URL 由撰寫網站登入程式碼的人決定。
- 有一個步驟需要你在網頁上登入 Apple 開發者帳號。

## 註冊 Services ID

在專案的聊天中詢問，例如：

```text
為我們網站上的「使用 Apple 登入」註冊一個 Services ID，名為「Notes Web」，識別碼為 com.example.notes.web。
```

Agent 用 `asc web service-ids create` 註冊它。該指令會變更你的 Apple 開發者帳號，因此只有加上 `--confirm` 才會執行，Agent 會先詢問你。你網站的登入程式碼會使用該識別碼，請記下它。

新的 Services ID 還沒有開啟任何功能。該指令的說明指出，要在 Developer Portal 中為它設定「使用 Apple 登入」，這就是下一步。

## 為它開啟「使用 Apple 登入」

`asc` 無法完成這個步驟；你需要在網頁上的 Apple 開發者帳號中操作。Apple 的[為網頁設定「使用 Apple 登入」](https://developer.apple.com/help/account/capabilities/configure-sign-in-with-apple-for-the-web/)頁面會逐步說明：打開 Services ID，開啟「使用 Apple 登入」，選擇你 App 的 App ID 作為其主要 App ID，然後輸入網站的網域和返回 URL。

## 設定網域和返回 URL

若要檢查網域和返回 URL，或日後變更它們，可以這樣詢問：

```text
把 com.example.notes.web 的「使用 Apple 登入」網域設為 example.com 和 www.example.com，返回 URL 為 https://example.com/auth/apple/callback。
```

Agent 使用 `asc web service-ids domains set`。它會用你提供的內容取代兩個清單，因此請列出你想保留的所有網域和返回 URL，而不只是新的。它只有加上 `--confirm` 才會執行，因此 Agent 會先詢問你。它會保持主要 App ID 不變，並且只有在重新讀取顯示已儲存的值後才回報成功。

| 值 | 指令接受的內容 |
| --- | --- |
| 網域 | 只能是主機名稱，例如 `example.com`：不含 `https://`、連接埠或路徑。國際化名稱使用 punycode。 |
| 返回 URL | 完整的 `https://` 位址，不含使用者名稱、密碼或 `#` 部分。每個位址的主機都必須在網域清單中。 |

## 為伺服器建立金鑰

詢問，例如：

```text
為 App 建立一個名為「Notes Web Key」的「使用 Apple 登入」金鑰，並告訴我你把它存在哪裡。
```

Agent 用 `asc web sign-in-keys create` 建立它，並與你 App 的 App ID 連結。Apple 的[撤銷、編輯和下載金鑰](https://developer.apple.com/help/account/keys/revoke-edit-and-download-keys/)頁面說明，金鑰檔案只能下載一次，而該指令會立即下載它，因此只有加上 `--confirm` 才會執行，Agent 會先詢問你。它會把檔案儲存到 Agent 指定的檔案夾中，位於它工作的電腦上，絕不會覆寫其他檔案，也絕不會印出金鑰。它會回報金鑰的 ID（金鑰 ID）、名稱以及檔案所在位置。

如果你已經有金鑰，請先詢問「我們有哪些『使用 Apple 登入』金鑰？」。`asc web sign-in-keys list` 會顯示每個金鑰的 ID、名稱，以及其檔案是否仍可下載。它也會列出用於其他 Apple 服務的金鑰。

## 把所需內容交給伺服器

你網站的登入程式碼需要：

- Services ID 的識別碼，來自第一步；
- 你的 Apple 團隊 ID，顯示在 **設定** › **開發者** 中該團隊下方；
- 金鑰 ID 和金鑰檔案。

Apple 的 [Sign in with Apple](https://developer.apple.com/documentation/sign_in_with_apple) 文件說明了伺服器如何使用它們。如果你的 Agent 在這個專案中撰寫網站，請讓它們用這些內容設定網站的登入。

若要在 AirBuild 以外執行的伺服器上使用該金鑰，請依下面的方法取一份副本並安裝到那裡。請妥善保管檔案：

- 請讓 Agent 把金鑰檔案保存在專案的 `.gitignore` 排除的檔案夾中，例如 `secrets/`，並告訴你它的路徑。這樣它就不會出現在儲存庫、分享連結和原始碼匯出中：請參閱[分享或匯出專案的原始碼](https://air.build/tw/docs/projects/share-and-export-source/#what-the-shared-source-contains)。它仍會以變暗的樣式顯示在 **檔案** 中，團隊中的任何人都可以打開它。
- 若要取一份副本，請在 **檔案** 中打開它並使用 **匯出**（請參閱[尋找 Agent 製作的檔案和文件](https://air.build/tw/docs/projects/find-files/#save-a-file-or-the-whole-project)），或讓 Agent 在你與它的私訊中把檔案傳給你（請參閱[建立群組聊天並與單一 Agent 對話](https://air.build/tw/docs/projects/groups-and-dms/#talk-to-one-agent)），然後按住該檔案（在 Mac 上按右鍵）並選擇 **另存為…**。不要在專案的聊天中索取金鑰：聊天中的每個人都能打開它，而你無法收回 Agent 傳送的訊息。
- 如果你的網站在 Vercel 上，由 Vercel Agent 變更環境設定。請參閱[讓 Agent 部署並檢查你的 Vercel 專案](https://air.build/tw/docs/connect/deploy-to-vercel-with-an-agent/)。

## 查看、變更或移除

| 詢問 | `asc` |
| --- | --- |
| 「我們有哪些 Services ID？」 | `web service-ids list` |
| 「把 Services ID com.example.notes.web 重新命名為 Notes Website。」 | `web service-ids rename`，只有加上 `--confirm` 才會執行，因此 Agent 會先詢問你。已開啟的功能保持不變。 |
| 「刪除 Services ID com.example.notes.old。」 | `web service-ids delete`，只有加上 `--confirm` 才會執行，因此 Agent 會先詢問你。 |
| 「再次下載 Notes Web Key 的檔案。」 | `web sign-in-keys download`，前提是清單顯示該檔案仍可下載，而且只有加上 `--confirm` 才會執行，因此 Agent 會先詢問你。 |
| 「撤銷舊的『使用 Apple 登入』金鑰。」 | Agent 無法執行：`asc` 不能撤銷金鑰。請在你的 Apple 開發者帳號中操作；請參閱 Apple 的[撤銷、編輯和下載金鑰](https://developer.apple.com/help/account/keys/revoke-edit-and-download-keys/)。 |

## 如果出了問題

**Agent 說要先在 Developer Portal 中設定「使用 Apple 登入」及其主要 App ID。**

訊息為「configure Sign in with Apple and its primary App ID in Developer Portal before setting domains」、「sign in with Apple must already be enabled before setting domains」和「services ID must already have a primary App ID configured」。請完成[為它開啟「使用 Apple 登入」](https://air.build/tw/docs/build/let-people-sign-in-with-apple-on-your-website/#turn-on-sign-in-with-apple-for-it)中的步驟，然後再次詢問。

**Agent 說返回 URL 的主機必須出現在網域中。**

訊息為 `--return-url hosts must appear in --domain`。請把返回 URL 的主機加入網域清單，或修正返回 URL。

**Agent 說某個網域或返回 URL 無效。**

訊息為 `--domain must contain DNS hostnames, without a scheme, port, or path`、`--domain must contain ASCII DNS hostnames (use punycode for international names)` 和 `--return-url must contain absolute HTTPS URLs without credentials or fragments`。網域請只寫主機名稱，返回 URL 請寫完整的 `https://` 位址；請參閱[設定網域和返回 URL](https://air.build/tw/docs/build/let-people-sign-in-with-apple-on-your-website/#set-the-domains-and-return-urls)中的表格。

**Agent 說更新已被接受，但無法核實。**

該訊息表示 Developer Portal 接受了 Services ID 的網域更新，但驗證失敗。請先讓 Agent 查看該 Services ID，再重試。

**Agent 說金鑰已存在，但其檔案沒有儲存。**

金鑰已建立，但下載或儲存失敗。不要要求新的金鑰。請讓 Agent 檢查金鑰清單；如果檔案仍可下載，請讓它用 `sign-in-keys download` 下載。

**Agent 說 Apple 拒絕了請求（403）。**

該訊息表示帳號角色可能沒有執行此操作的權限，或網頁工作階段已過期。請檢查擁有者在 Apple 團隊中的角色：Apple 的[註冊服務 ID](https://developer.apple.com/help/account/identifiers/register-a-services-id/)頁面要求帳號持有人或管理員角色。如有提示，請讓擁有者在 **設定** › **開發者** 中重新登入。請參閱[連結你的 Apple 開發者帳號](https://air.build/tw/docs/app-store/connect-apple-developer-account/#sign-in-again)。

**Agent 說 Apple 拒絕了網頁工作階段。**

Apple 不接受 AirBuild 中保存的團隊擁有者的 Apple 登入。Agent 會再試一次；如果仍然失敗，會請擁有者重新登入。請參閱[連結你的 Apple 開發者帳號](https://air.build/tw/docs/app-store/connect-apple-developer-account/#sign-in-again)。
