您可以將驗證提供者憑證連結至現有的使用者帳戶,讓使用者透過多個驗證服務提供者登入您的應用程式。無論用來登入的驗證服務供應商為何,系統都會透過相同的 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); });