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

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

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

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

วิธีลิงก์ข้อมูลเข้าสู่ระบบของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่

  1. ลงชื่อเข้าใช้ผู้ใช้โดยใช้ผู้ให้บริการหรือวิธีการตรวจสอบสิทธิ์ใดก็ได้
  2. ดำเนินการตามขั้นตอนการลงชื่อเข้าใช้ของผู้ให้บริการการตรวจสอบสิทธิ์รายใหม่จนเสร็จสิ้น แต่ไม่ใช่ ซึ่งรวมถึงการเรียกใช้วิธีใดวิธีหนึ่งของ firebase::auth::Auth::SignInWithCredential เช่น ดู โทเค็น Google ID, โทเค็นเพื่อการเข้าถึง Facebook หรืออีเมลและรหัสผ่านของผู้ใช้
  3. รับ firebase::auth::Credential สำหรับผู้ให้บริการตรวจสอบสิทธิ์รายใหม่:

    Google Sign-In
    firebase::auth::Credential credential =
        firebase::auth::GoogleAuthProvider::GetCredential(google_id_token,
                                                          nullptr);
    
    การเข้าสู่ระบบ Facebook
    firebase::auth::Credential credential =
        firebase::auth::FacebookAuthProvider::GetCredential(access_token);
    
    การลงชื่อเข้าใช้ด้วยอีเมลด้วยรหัสผ่าน
    firebase::auth::Credential credential =
        firebase::auth::EmailAuthProvider::GetCredential(email, password);
    
  4. ส่งออบเจ็กต์ firebase::auth::Credential ไปยังผู้ใช้ที่ลงชื่อเข้าใช้ เมธอด LinkWithCredential:

    // Link the new credential to the currently active user.
    firebase::auth::User current_user = auth->current_user();
    firebase::Future<firebase::auth::AuthResult> result =
        current_user.LinkWithCredential(credential);
    

    การเรียก LinkWithCredential จะล้มเหลวหากข้อมูลเข้าสู่ระบบคือ ลิงก์กับบัญชีผู้ใช้อื่นแล้ว ในกรณีนี้ คุณต้องจัดการ การผสานรวมบัญชีและข้อมูลที่เกี่ยวข้องตามความเหมาะสมสำหรับแอปของคุณ

    // Gather data for the currently signed in User.
    firebase::auth::User current_user = auth->current_user();
    std::string current_email = current_user.email();
    std::string current_provider_id = current_user.provider_id();
    std::string current_display_name = current_user.display_name();
    std::string current_photo_url = current_user.photo_url();
    
    // Sign in with the new credentials.
    firebase::Future<firebase::auth::AuthResult> result =
        auth->SignInAndRetrieveDataWithCredential(credential);
    
    // To keep example simple, wait on the current thread until call completes.
    while (result.status() == firebase::kFutureStatusPending) {
      Wait(100);
    }
    
    // The new User is now active.
    if (result.error() == firebase::auth::kAuthErrorNone) {
      firebase::auth::User* new_user = *result.result();
    
      // Merge new_user with the user in details.
      // ...
      (void)new_user;
    }
    

หากโทรหา LinkWithCredential สำเร็จ ผู้ใช้จะสามารถลงชื่อเข้าใช้โดยใช้ ผู้ให้บริการการตรวจสอบสิทธิ์ที่ลิงก์อยู่รายใดก็ได้ และเข้าถึงข้อมูล Firebase เดียวกัน

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

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

// Unlink the sign-in provider from the currently active user.
firebase::auth::User current_user = auth->current_user();
firebase::Future<firebase::auth::AuthResult> result =
    current_user.Unlink(providerId);