您可以允許用戶使用多個身份驗證提供程序登錄您的應用程序,方法是將身份驗證提供程序憑據鏈接到現有用戶帳戶。用戶可以通過相同的 Firebase 用戶 ID 進行識別,而不管他們用於登錄的身份驗證提供程序如何。例如,使用密碼登錄的用戶可以關聯 Google 帳戶並在未來使用任一方法登錄。或者,匿名用戶可以鏈接 Facebook 帳戶,然後使用 Facebook 登錄以繼續使用您的應用程序。
在你開始之前
向您的應用程序添加對兩個或更多身份驗證提供程序(可能包括匿名身份驗證)的支持。
FirebaseAuth
類是所有 API 調用的網關。它可以通過FirebaseAuth.DefaultInstance訪問。Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
將身份驗證提供者憑據鏈接到用戶帳戶
要將身份驗證提供者憑據鏈接到現有用戶帳戶:
- 使用任何身份驗證提供程序或方法登錄用戶。
- 完成新身份驗證提供程序的登錄流程,直到但不包括調用
Firebase.Auth.FirebaseAuth.SignInAndRetrieveDataWithCredentialAsync
方法之一。例如,獲取用戶的 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.AuthResult result = task.Result; Debug.LogFormat("Credentials successfully linked to Firebase user: {0} ({1})", result.User.DisplayName, result.User.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.SignInAndRetrieveDataWithCredentialAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SignInAndRetrieveDataWithCredentialAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SignInAndRetrieveDataWithCredentialAsync encountered an error: " + task.Exception); return; } Firebase.Auth.AuthResult result = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", result.User.DisplayName, result.User.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.AuthResult result = task.Result; Debug.LogFormat("Credentials successfully unlinked from user: {0} ({1})", result.User.DisplayName, result.User.UserId); });