将多个身份验证提供方与一个账号相关联
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以通过将多个身份验证提供方凭据关联至现有用户账号,让用户可以使用多个身份验证提供方登录您的应用。无论用户使用哪个身份验证提供方登录,系统均可通过同一 Firebase 用户 ID 识别用户。例如,使用密码登录的用户可以关联 Google 账号,以后便可使用这两种方法中的任意一种登录。或者,匿名用户可以关联 Facebook 账号,以后就可以使用 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()
方法。您可以从 User
对象的 providerData
属性中获取与用户相关联的身份验证提供方的 ID。
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.");
}
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-04。
[null,null,["最后更新时间 (UTC):2025-08-04。"],[],[],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 }"]]