將多個驗證供應商連結至 Unity 中的帳戶

您可以將驗證供應商憑證連結至現有使用者帳戶,允許使用者透過多個驗證供應商登入應用程式。無論使用者透過哪個驗證提供者登入,系統都會使用相同的 Firebase 使用者 ID 識別使用者。舉例來說,以密碼登入的使用者可以連結 Google 帳戶,日後就能透過任一方法登入。或者,匿名使用者可以連結 Facebook 帳戶,然後稍後使用 Facebook 登入,繼續使用您的應用程式。

事前準備

在應用程式中新增對兩個以上驗證供應商的支援 (可能包括匿名驗證)。

FirebaseAuth 類別是所有 API 呼叫的閘道。 可透過 FirebaseAuth.DefaultInstance 存取。
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;

如要將驗證供應商憑證連結至現有使用者帳戶,請按照下列步驟操作:

  1. 使用任何驗證供應商或方法登入使用者。
  2. 完成新驗證供應商的登入流程,但不要呼叫 Firebase.Auth.FirebaseAuth.SignInAndRetrieveDataWithCredentialAsync 方法。例如,取得使用者的 Google ID 權杖、Facebook 存取權杖,或是電子郵件和密碼。
  3. 取得新驗證供應商的 Firebase.Auth.Credential

    Google 登入
    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);
  4. 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);
});

疑難排解

如果嘗試連結多個帳戶時發生錯誤,請參閱已驗證電子郵件地址的說明文件。