ลิงก์ผู้ให้บริการการตรวจสอบสิทธิ์หลายรายกับบัญชีใน Unity

คุณอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปโดยใช้การตรวจสอบสิทธิ์หลายรายการได้ ผู้ให้บริการการตรวจสอบสิทธิ์ได้โดยลิงก์ข้อมูลเข้าสู่ระบบของผู้ให้บริการการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่ สามารถระบุผู้ใช้ได้ด้วยรหัสผู้ใช้ Firebase เดียวกัน โดยไม่คำนึงถึง ของผู้ให้บริการตรวจสอบสิทธิ์ที่ตนใช้ในการลงชื่อเข้าใช้ เช่น ผู้ใช้ที่ลงชื่อเข้าใช้ ด้วยรหัสผ่าน สามารถลิงก์บัญชี Google และลงชื่อเข้าใช้ด้วยวิธีใดวิธีหนึ่งใน ในอนาคต หรือผู้ใช้ที่ไม่ระบุชื่อสามารถเชื่อมโยงบัญชี Facebook แล้วลงชื่อเข้าใช้ในภายหลัง กับ Facebook เพื่อใช้แอปของคุณต่อไป

ก่อนเริ่มต้น

เพิ่มการรองรับผู้ให้บริการการตรวจสอบสิทธิ์ 2 รายขึ้นไป (โดยอาจรวมถึง การตรวจสอบสิทธิ์แบบไม่ระบุชื่อ) ไปยังแอปของคุณ

คลาส 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 Sign-In
    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 เดียวกัน

คุณสามารถยกเลิกการลิงก์ผู้ให้บริการตรวจสอบสิทธิ์กับบัญชี เพื่อไม่ให้ผู้ใช้ ลงชื่อเข้าใช้กับผู้ให้บริการรายนั้นได้นานขึ้น

หากต้องการยกเลิกการลิงก์ผู้ให้บริการตรวจสอบสิทธิ์จากบัญชีผู้ใช้ โปรดส่งรหัสผู้ให้บริการไปยัง UnlinkAsync วิธี คุณสามารถรับรหัสผู้ให้บริการของการตรวจสอบสิทธิ์ ผู้ให้บริการที่ลิงก์กับผู้ใช้โดยการโทร ProviderData

// 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);
});

Firebase gives you the tools and infrastructure you need to build better mobile and web apps, improve app quality, and grow your business.

อัปเดตแล้ว Feb 28, 2025

Firebase gives you the tools and infrastructure you need to build better mobile and web apps, improve app quality, and grow your business.

อัปเดตแล้ว Feb 28, 2025

Firebase gives you the tools and infrastructure you need to build better mobile and web apps, improve app quality, and grow your business.

อัปเดตแล้ว Feb 28, 2025