เชื่อมโยงผู้ให้บริการรับรองความถูกต้องหลายรายเข้ากับบัญชีโดยใช้ C ++

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

ก่อนที่คุณจะเริ่ม

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

หากต้องการเชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการรับรองความถูกต้องกับบัญชีผู้ใช้ที่มีอยู่:

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

    ลงชื่อเข้าใช้ Google
    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 เดียวกันได้

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

หากต้องการยกเลิกการเชื่อมโยงผู้ให้บริการรับรองความถูกต้องจากบัญชีผู้ใช้ ให้ส่ง ID ผู้ให้บริการไปยังวิธี Unlink คุณสามารถรับ ID ผู้ให้บริการของผู้ให้บริการการตรวจสอบสิทธิ์ที่เชื่อมโยงกับผู้ใช้ได้โดยโทรไปที่ 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);