將多個驗證供應商連結至一個帳戶
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以讓使用者透過多重驗證機制登入您的應用程式
將驗證服務供應商憑證連結至現有使用者帳戶。
無論
用於登入的驗證服務供應商例如登入後
使用密碼可以連結 Google 帳戶,並在
或者,匿名使用者也可以連結 Facebook 帳戶,之後再登入
以繼續使用應用程式。
事前準備
為兩個以上的驗證供應商新增支援 (可能包括
匿名驗證)。
將驗證服務供應商憑證連結至使用者帳戶
如要將驗證提供者憑證與現有使用者帳戶建立連結,請按照下列步驟操作:
使用任何驗證提供者或方式登入使用者。
完成新驗證供應商的登入流程
包括呼叫其中一種 signInWith
方法舉例來說,取得
使用者的 Google ID 權杖、Facebook 存取權杖或電子郵件地址和密碼。
取得新驗證供應商的 Credential
物件:
// Google Sign-in
final credential = GoogleAuthProvider.credential(idToken: idToken);
// Email and password sign-in
final credential =
EmailAuthProvider.credential(email: emailAddress, password: password);
// Etc.
將 Credential
物件傳遞至登入使用者的 linkWithCredential()
方法:
try {
final userCredential = await FirebaseAuth.instance.currentUser
?.linkWithCredential(credential);
} on FirebaseAuthException catch (e) {
switch (e.code) {
case "provider-already-linked":
print("The provider has already been linked to the user.");
break;
case "invalid-credential":
print("The provider's credential is not valid.");
break;
case "credential-already-in-use":
print("The account corresponding to the credential already exists, "
"or is already linked to a Firebase User.");
break;
// See the API reference for the full list of error codes.
default:
print("Unknown error.");
}
```
如果呼叫 linkWithCredential()
成功,使用者現在可以使用
任何已連結的驗證服務供應商,並存取相同的 Firebase 資料。
取消驗證提供者與使用者帳戶的連結
您可以取消驗證供應商與帳戶的連結,這樣使用者就無法
以在該平台登入帳戶
如要取消驗證提供者與使用者帳號的連結,請將提供者 ID 傳遞至
unlink()
方法。您可以取得連結至驗證供應商的供應商 ID
來自 User
物件的 providerData
屬性的使用者。
try {
await FirebaseAuth.instance.currentUser?.unlink(providerId);
} on FirebaseAuthException catch (e) {
switch (e.code) {
case "no-such-provider":
print("The user isn't linked to the provider or the provider "
"doesn't exist.");
break;
default:
print("Unknown error.");
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],[],null,["# Link Multiple Auth Providers to an Account\n\n\u003cbr /\u003e\n\nYou can allow users to sign in to your app using multiple authentication\nproviders by linking auth provider credentials to an existing user account.\nUsers are identifiable by the same Firebase user ID regardless of the\nauthentication provider they used to sign in. For example, a user who signed in\nwith a password can link a Google account and sign in with either method in the\nfuture. Or, an anonymous user can link a Facebook account and then, later, sign\nin with Facebook to continue using your app.\n\nBefore you begin\n----------------\n\nAdd support for two or more authentication providers (possibly including\nanonymous authentication) to your app.\n\nLink auth provider credentials to a user account\n------------------------------------------------\n\nTo link auth provider credentials to an existing user account:\n\n1. Sign in the user using any authentication provider or method.\n\n2. Complete the sign-in flow for the new authentication provider up to, but not\n including, calling one of the `signInWith`- methods. For example, get\n the user's Google ID token, Facebook access token, or email and password.\n\n3. Get a `Credential` object for the new authentication provider:\n\n // Google Sign-in\n final credential = GoogleAuthProvider.credential(idToken: idToken);\n\n // Email and password sign-in\n final credential =\n EmailAuthProvider.credential(email: emailAddress, password: password);\n\n // Etc.\n\n4. Pass the `Credential` object to the sign-in user's `linkWithCredential()`\n method:\n\n try {\n final userCredential = await FirebaseAuth.instance.currentUser\n ?.linkWithCredential(credential);\n } on FirebaseAuthException catch (e) {\n switch (e.code) {\n case \"provider-already-linked\":\n print(\"The provider has already been linked to the user.\");\n break;\n case \"invalid-credential\":\n print(\"The provider's credential is not valid.\");\n break;\n case \"credential-already-in-use\":\n print(\"The account corresponding to the credential already exists, \"\n \"or is already linked to a Firebase User.\");\n break;\n // See the API reference for the full list of error codes.\n default:\n print(\"Unknown error.\");\n }\n ```\n\nIf the call to `linkWithCredential()` succeeds, the user can now sign in using\nany linked authentication provider and access the same Firebase data.\n\nUnlink an auth provider from a user account\n-------------------------------------------\n\nYou can unlink an auth provider from an account, so that the user can no\nlonger sign in with that provider.\n\nTo unlink an auth provider from a user account, pass the provider ID to the\n`unlink()` method. You can get the provider IDs of the auth providers linked to\na user from the `User` object's `providerData` property. \n\n try {\n await FirebaseAuth.instance.currentUser?.unlink(providerId);\n } on FirebaseAuthException catch (e) {\n switch (e.code) {\n case \"no-such-provider\":\n print(\"The user isn't linked to the provider or the provider \"\n \"doesn't exist.\");\n break;\n default:\n print(\"Unknown error.\");\n }\n }"]]