您可以使用 Firebase CLI 透過 firebase.json 設定檔,為 Firebase 專案啟用及設定 Authentication 供應商。這樣一來,您就能以程式碼形式管理 Authentication 設定,並與其他 Firebase 資產一起部署。
步驟 1:設定Authentication供應商
如要使用 CLI 設定 Authentication 供應商,請從專案目錄執行下列指令:
firebase init auth
這項指令會提示您選取要啟用的供應商,並要求提供各供應商的必要設定詳細資料。您可以使用 Firebase CLI 啟用 Google 登入、電子郵件/密碼或匿名驗證。
步驟 2:檢查 firebase.json 設定
初始化後,firebase.json 檔案會包含 auth 區段和供應商設定。您也可以手動編輯這個檔案,新增或更新供應商。
下表說明可透過 Firebase CLI 設定的 Authentication 供應商可用欄位:
{
"auth": {
"providers": {
"anonymous": true,
"emailPassword": true,
"googleSignIn": {
"oAuthBrandDisplayName": "My App",
"supportEmail": "support@myapp.com",
"authorizedRedirectUris": [
"https://myapp.com",
"http://localhost:4000"
]
}
}
}
}
供應商設定欄位
下表說明 Authentication 供應商可用的欄位:
| 欄位 | 說明 |
|---|---|
anonymous |
布林值。設為 true 即可啟用匿名驗證。 |
emailPassword |
布林值。設為 true 即可啟用電子郵件地址/密碼驗證。 |
googleSignIn |
Object。Google 登入設定。 |
googleSignIn.oAuthBrandDisplayName |
字串。使用 Google 登入時,OAuth 品牌的顯示名稱。 |
googleSignIn.supportEmail |
字串。使用 Google 登入功能時,為 OAuth 品牌註冊的支援電子郵件地址。 |
googleSignIn.authorizedRedirectUris |
陣列。Google 登入的授權重新導向 URI 清單。如果您使用 Firebase Hosting 搭配預設網域,系統會自動授權。 |
步驟 3:部署 Authentication 設定
如要將 Authentication 設定套用至 Firebase 專案,請執行下列指令:
firebase deploy --only auth
這個指令會啟用已設定的供應商,並更新 Firebase 專案中的設定。