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