您可以透過將身分驗證提供者憑證連結到現有使用者帳戶,允許使用者使用多個驗證提供者登入您的應用程式。無論使用者用於登入的驗證提供者為何,都可以透過相同的 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); });