คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายโดยเชื่อมโยงข้อมูลรับรองของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่ ID ผู้ใช้ Firebase เดียวกันสามารถระบุผู้ใช้ได้โดยไม่คำนึงถึงผู้ให้บริการตรวจสอบสิทธิ์ที่พวกเขาเคยลงชื่อเข้าใช้ ตัวอย่างเช่น ผู้ใช้ที่ลงชื่อเข้าใช้ด้วยรหัสผ่านสามารถเชื่อมโยงบัญชี Google และลงชื่อเข้าใช้ด้วยวิธีใดวิธีหนึ่งในอนาคต หรือผู้ใช้ที่ไม่ระบุชื่อสามารถเชื่อมโยงบัญชี Facebook แล้วลงชื่อเข้าใช้ด้วย Facebook ในภายหลังเพื่อใช้แอพของคุณต่อไป
ก่อนจะเริ่ม
เพิ่มการรองรับผู้ให้บริการตรวจสอบสิทธิ์สองรายขึ้นไป (อาจรวมถึงการตรวจสอบสิทธิ์แบบไม่ระบุตัวตน) ในแอปของคุณ
คลาสFirebaseAuth
เป็นเกตเวย์สำหรับการเรียก API ทั้งหมด สามารถเข้าถึงได้ผ่าน FirebaseAuth.DefaultInstanceFirebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
เชื่อมโยงข้อมูลรับรองผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้
วิธีเชื่อมโยงข้อมูลรับรองผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่:
- ลงชื่อเข้าใช้ผู้ใช้โดยใช้ผู้ให้บริการหรือวิธีการตรวจสอบสิทธิ์
- ทำตามขั้นตอนการลงชื่อเข้าใช้สำหรับผู้ให้บริการการตรวจสอบสิทธิ์ใหม่ให้เสร็จสิ้น แต่ไม่รวมการเรียกหนึ่งในเมธอด
Firebase.Auth.FirebaseAuth.SignInWithCredentialAsync
ตัวอย่างเช่น รับโทเค็น Google ID ของผู้ใช้ โทเค็นการเข้าถึง Facebook หรืออีเมลและรหัสผ่าน รับ
ลงชื่อเข้าใช้ GoogleFirebase.Auth.Credential
สำหรับผู้ให้บริการตรวจสอบสิทธิ์ใหม่:Firebase.Auth.Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(googleIdToken, googleAccessToken);
เข้าสู่ระบบ FacebookFirebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvider.GetCredential(accessToken);
อีเมล - ลงชื่อเข้าใช้ด้วยรหัสผ่านFirebase.Auth.Credential credential = Firebase.Auth.EmailAuthProvider.GetCredential(email, password);
ส่งผ่านอ็อบเจ็กต์
Firebase.Auth.Credential
ไปยังเมธอดLinkWithCredentialAsync
ของผู้ใช้ที่ลงชื่อเข้าใช้:auth.CurrentUser.LinkWithCredentialAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("LinkWithCredentialAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("LinkWithCredentialAsync encountered an error: " + task.Exception); return; } Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("Credentials successfully linked to Firebase user: {0} ({1})", newUser.DisplayName, newUser.UserId); });
การเรียก
LinkWithCredentialAsync
จะล้มเหลวหากข้อมูลประจำตัวเชื่อมโยงกับบัญชีผู้ใช้อื่นอยู่แล้ว ในสถานการณ์นี้ คุณต้องจัดการการรวมบัญชีและข้อมูลที่เกี่ยวข้องตามความเหมาะสมกับแอปของคุณ:// Gather data for the currently signed in User. string currentUserId = auth.CurrentUser.UserId; string currentEmail = auth.CurrentUser.Email; string currentDisplayName = auth.CurrentUser.DisplayName; System.Uri currentPhotoUrl = auth.CurrentUser.PhotoUrl; // Sign in with the new credentials. auth.SignInWithCredentialAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SignInWithCredentialAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception); return; } Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId); // TODO: Merge app specific details using the newUser and values from the // previous user, saved above. });
หากการเรียก LinkWithCredentialAsync
สำเร็จ ผู้ใช้สามารถลงชื่อเข้าใช้โดยใช้ผู้ให้บริการตรวจสอบสิทธิ์ที่เชื่อมโยงและเข้าถึงข้อมูล Firebase เดียวกันได้
ยกเลิกการเชื่อมโยงผู้ให้บริการตรวจสอบสิทธิ์จากบัญชีผู้ใช้
คุณสามารถยกเลิกการเชื่อมโยงผู้ให้บริการตรวจสอบสิทธิ์จากบัญชี เพื่อให้ผู้ใช้ไม่สามารถลงชื่อเข้าใช้กับผู้ให้บริการนั้นได้อีกต่อไป
หากต้องการยกเลิกการเชื่อมโยงผู้ให้บริการตรวจสอบสิทธิ์จากบัญชีผู้ใช้ ให้ส่ง ID ผู้ให้บริการไปที่วิธี UnlinkAsync
คุณสามารถรับ ID ผู้ให้บริการของผู้ให้บริการตรวจสอบสิทธิ์ที่เชื่อมโยงกับผู้ใช้โดยโทรไปที่ ProviderData
// Unlink the sign-in provider from the currently active user. // providerIdString is a string identifying a provider, // retrieved via FirebaseAuth.FetchProvidersForEmail(). auth.CurrentUser.UnlinkAsync(providerIdString).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UnlinkAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UnlinkAsync encountered an error: " + task.Exception); return; } // The user has been unlinked from the provider. Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("Credentials successfully unlinked from user: {0} ({1})", newUser.DisplayName, newUser.UserId); });