您可以通過將身份驗證提供程序憑據鏈接到現有用戶帳戶來允許用戶使用多個身份驗證提供程序登錄您的應用程序。用戶可以通過相同的 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); });