您可以通过将身份验证提供方凭据关联至现有用户帐号,允许用户使用多个身份验证提供方服务登录您的应用。无论用户使用哪个身份验证提供方服务登录,均可通过同一 Firebase 用户 ID 识别用户。例如,使用密码登录的用户可以关联 Google 帐号,以后便可使用这两种方法中的任意一种登录。或者,匿名用户可以关联 Facebook 帐号,以后就可以使用 Facebook 登录,然后继续使用您的应用。
准备工作
为您的应用增加对两个或多个身份验证提供方服务(可能包括匿名身份验证)的支持。
FirebaseAuth
类是所有 API 调用都需要通过的门户,它可以通过 FirebaseAuth.DefaultInstance 访问。
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
将身份验证提供方凭据与用户帐号关联
要将身份验证提供方凭据与现有用户帐号关联,请按以下步骤操作:
- 使用任意身份验证提供方服务或方法登录用户。
- 按照新身份验证提供方的登录流程逐步进行,直到需要调用某一
Firebase.Auth.FirebaseAuth.SignInWithCredentialAsync
方法的前一步时停止。调用的例子包括:获取该用户的 Google ID 令牌、Facebook 访问令牌或电子邮件地址和密码。 为新的身份验证提供方获取
Google 登录Firebase.Auth.Credential
:Firebase.Auth.Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(googleIdToken, googleAccessToken);
Facebook 登录Firebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvider.GetCredential(accessToken);
电子邮件地址与密码登录Firebase.Auth.Credential credential = Firebase.Auth.EmailAuthProvider.GetCredential(email, password);
将
Firebase.Auth.Credential
对象传递给已登录用户的LinkWithCredentialAsync
方法:auth.CurrentUser.LinkWithCredentialAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("LinkWithCredentialAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("LinkWithCredentialAsync encountered an error: " + task.Exception); return; } Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("Credentials successfully linked to Firebase user: {0} ({1})", newUser.DisplayName, newUser.UserId); });
如果凭据已与另一用户帐号关联,则无法调用
LinkWithCredentialAsync
。在这种情况下,您必须根据需要为您的应用合并帐号和相关数据:// Gather data for the currently signed in User. string currentUserId = auth.CurrentUser.UserId; string currentEmail = auth.CurrentUser.Email; string currentDisplayName = auth.CurrentUser.DisplayName; System.Uri currentPhotoUrl = auth.CurrentUser.PhotoUrl; // Sign in with the new credentials. auth.SignInWithCredentialAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SignInWithCredentialAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception); return; } Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId); // TODO: Merge app specific details using the newUser and values from the // previous user, saved above. });
如果成功调用 LinkWithCredentialAsync
,用户现在便可使用关联的任意身份验证提供方服务登录,并使用相同的 Firebase 数据。
取消身份验证提供方服务与用户帐号的关联
您可以取消身份验证提供方服务与帐号的关联,这样用户就无法再通过该提供方服务登录了。
要取消身份验证提供方服务与用户帐号的关联,请将提供方 ID 传递给 UnlinkAsync
方法。您可以通过调用 ProviderData
获取与用户相关联的身份验证提供方服务的提供方 ID。
// Unlink the sign-in provider from the currently active user. // providerIdString is a string identifying a provider, // retrieved via FirebaseAuth.FetchProvidersForEmail(). auth.CurrentUser.UnlinkAsync(providerIdString).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UnlinkAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UnlinkAsync encountered an error: " + task.Exception); return; } // The user has been unlinked from the provider. Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("Credentials successfully unlinked from user: {0} ({1})", newUser.DisplayName, newUser.UserId); });