คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายโดยเชื่อมโยงข้อมูลรับรองของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่ ID ผู้ใช้ Firebase เดียวกันสามารถระบุผู้ใช้ได้โดยไม่คำนึงถึงผู้ให้บริการตรวจสอบสิทธิ์ที่พวกเขาเคยลงชื่อเข้าใช้ ตัวอย่างเช่น ผู้ใช้ที่ลงชื่อเข้าใช้ด้วยรหัสผ่านสามารถเชื่อมโยงบัญชี Google และลงชื่อเข้าใช้ด้วยวิธีใดวิธีหนึ่งในอนาคต หรือผู้ใช้ที่ไม่ระบุชื่อสามารถเชื่อมโยงบัญชี Facebook แล้วลงชื่อเข้าใช้ด้วย Facebook ในภายหลังเพื่อใช้แอพของคุณต่อไป
ก่อนจะเริ่ม
เพิ่มการสนับสนุนสำหรับผู้ให้บริการตรวจสอบสิทธิ์สองรายขึ้นไป (อาจรวมถึงการตรวจสอบสิทธิ์แบบไม่ระบุตัวตน) ในแอปของคุณ
เชื่อมโยงข้อมูลรับรองผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้
วิธีเชื่อมโยงข้อมูลรับรองผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่:
- ลงชื่อเข้าใช้ผู้ใช้โดยใช้ผู้ให้บริการหรือวิธีการตรวจสอบสิทธิ์
- ทำตามขั้นตอนการลงชื่อเข้าใช้สำหรับผู้ให้บริการการตรวจสอบสิทธิ์ใหม่ให้สมบูรณ์ แต่ไม่รวมการเรียกหนึ่งในวิธี
firebase::auth::Auth::SignInWithCredential
ตัวอย่างเช่น รับโทเค็น Google ID ของผู้ใช้ โทเค็นการเข้าถึง Facebook หรืออีเมลและรหัสผ่าน รับ
ลงชื่อเข้าใช้ Googlefirebase::auth::Credential
สำหรับผู้ให้บริการตรวจสอบสิทธิ์ใหม่:firebase::auth::Credential credential = firebase::auth::GoogleAuthProvider::GetCredential(google_id_token, nullptr);
เข้าสู่ระบบ Facebookfirebase::auth::Credential credential = firebase::auth::FacebookAuthProvider::GetCredential(access_token);
อีเมล - ลงชื่อเข้าใช้ด้วยรหัสผ่านfirebase::auth::Credential credential = firebase::auth::EmailAuthProvider::GetCredential(email, password);
ส่งอ็อบเจ็กต์
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::User*> 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::User*> result = auth->SignInWithCredential(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::User*> result = current_user->Unlink(providerId);