คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายโดยเชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่ สามารถระบุผู้ใช้ได้ด้วย ID ผู้ใช้ Firebase เดียวกัน โดยไม่คำนึงถึงผู้ให้บริการการตรวจสอบสิทธิ์ที่ใช้ลงชื่อเข้าใช้ ตัวอย่างเช่น ผู้ใช้ที่ลงชื่อเข้าใช้ด้วยรหัสผ่านสามารถเชื่อมโยงบัญชี Google และลงชื่อเข้าใช้ด้วยวิธีใดวิธีหนึ่งในอนาคต หรือผู้ใช้ที่ไม่ระบุชื่อสามารถเชื่อมโยงบัญชี Facebook จากนั้นเข้าสู่ระบบด้วย Facebook ในภายหลังเพื่อใช้แอพของคุณต่อไป
ก่อนที่คุณจะเริ่มต้น
เพิ่มการรองรับผู้ให้บริการตรวจสอบสิทธิ์ตั้งแต่ 2 รายขึ้นไป (อาจรวมถึงการตรวจสอบสิทธิ์แบบไม่ระบุตัวตน) ให้กับแอปของคุณ
เชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการรับรองความถูกต้องกับบัญชีผู้ใช้
ในการเชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการรับรองความถูกต้องกับบัญชีผู้ใช้ที่มีอยู่:
- ลงชื่อเข้าใช้ผู้ใช้โดยใช้ผู้ให้บริการหรือวิธีการรับรองความถูกต้องใดๆ
- ดำเนินการขั้นตอนการลงชื่อเข้าใช้สำหรับผู้ให้บริการการรับรองความถูกต้องใหม่ให้เสร็จสมบูรณ์ แต่ไม่รวมถึงการเรียกหนึ่งในเมธอด
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
object ไปยังเมธอด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);