您可以將驗證供應商憑證連結至現有使用者帳戶,允許使用者透過多個驗證供應商登入應用程式。無論使用者透過哪個驗證提供者登入,系統都會使用相同的 Firebase 使用者 ID 識別使用者。舉例來說,以密碼登入的使用者可以連結 Google 帳戶,日後就能透過任一方法登入。或者,匿名使用者可以連結 Facebook 帳戶,然後稍後使用 Facebook 登入,繼續使用您的應用程式。
事前準備
在應用程式中新增對兩個以上驗證供應商的支援 (可能包括匿名驗證)。
將驗證供應商憑證連結至使用者帳戶
如要將驗證供應商憑證連結至現有使用者帳戶,請按照下列步驟操作:
使用任何驗證供應商或方法登入使用者。
完成新驗證提供者的登入流程,但不要呼叫任何 signInWith 方法。例如,取得使用者的 Google ID 權杖、Facebook 存取權杖,或是電子郵件和密碼。
取得新驗證供應商的 Credential 物件:
// Google Sign-infinalcredential=GoogleAuthProvider.credential(idToken:idToken);// Email and password sign-infinalcredential=EmailAuthProvider.credential(email:emailAddress,password:password);// Etc.
將 Credential 物件傳遞至登入使用者的 linkWithCredential() 方法:
try{finaluserCredential=awaitFirebaseAuth.instance.currentUser?.linkWithCredential(credential);}onFirebaseAuthExceptioncatch(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.");}```
如要取消授權提供者與使用者帳戶的連結,請將提供者 ID 傳遞至 unlink() 方法。您可以從 User 物件的 providerData 屬性,取得連結至使用者的驗證提供者 ID。
try{awaitFirebaseAuth.instance.currentUser?.unlink(providerId);}onFirebaseAuthExceptioncatch(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.");}}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-28 (世界標準時間)。"],[],[],null,["\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\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\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\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 }\n\nTroubleshooting\n\nIf you encounter errors when trying to link multiple accounts, see the\n[documentation on verified email addresses](https://firebase.google.com/docs/auth/users#verified_email_addresses)."]]