คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายได้โดยการเชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่ ผู้ใช้สามารถระบุตัวผู้ใช้ได้ด้วย ID ผู้ใช้ Firebase เดียวกัน ไม่ว่าผู้ให้บริการตรวจสอบสิทธิ์ที่พวกเขาใช้ลงชื่อเข้าใช้จะเป็นคนใดก็ตาม ตัวอย่างเช่น ผู้ใช้ที่ลงชื่อเข้าใช้ด้วยรหัสผ่านสามารถเชื่อมโยงบัญชี Google และลงชื่อเข้าใช้ด้วยวิธีใดวิธีหนึ่งได้ในอนาคต หรือผู้ใช้ที่ไม่ระบุชื่อสามารถเชื่อมโยงบัญชี Facebook จากนั้นลงชื่อเข้าใช้ด้วย Facebook เพื่อใช้แอปของคุณต่อไปได้ในภายหลัง
ก่อนที่คุณจะเริ่ม
เพิ่มการรองรับผู้ให้บริการตรวจสอบสิทธิ์ตั้งแต่ 2 รายขึ้นไป (อาจรวมถึงการตรวจสอบสิทธิ์แบบไม่ระบุชื่อ) ให้กับแอปของคุณ
คลาสFirebaseAuth
เป็นเกตเวย์สำหรับการเรียก API ทั้งหมด สามารถเข้าถึงได้ผ่าน FirebaseAuth.DefaultInstanceFirebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
เชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการรับรองความถูกต้องกับบัญชีผู้ใช้
หากต้องการเชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการรับรองความถูกต้องกับบัญชีผู้ใช้ที่มีอยู่:
- ลงชื่อเข้าใช้ผู้ใช้โดยใช้ผู้ให้บริการหรือวิธีการตรวจสอบความถูกต้อง
- ดำเนินการขั้นตอนการลงชื่อเข้าใช้สำหรับผู้ให้บริการการตรวจสอบสิทธิ์ใหม่ให้เสร็จสมบูรณ์ แต่ไม่รวมการเรียกหนึ่งในเมธอด
Firebase.Auth.FirebaseAuth.SignInAndRetrieveDataWithCredentialAsync
ตัวอย่างเช่น รับโทเค็น 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.AuthResult result = task.Result; Debug.LogFormat("Credentials successfully linked to Firebase user: {0} ({1})", result.User.DisplayName, result.User.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.SignInAndRetrieveDataWithCredentialAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SignInAndRetrieveDataWithCredentialAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SignInAndRetrieveDataWithCredentialAsync encountered an error: " + task.Exception); return; } Firebase.Auth.AuthResult result = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", result.User.DisplayName, result.User.UserId); // TODO: Merge app specific details using the newUser and values from the // previous user, saved above. });
หากการเรียกไปยัง LinkWithCredentialAsync
สำเร็จ ขณะนี้ผู้ใช้สามารถลงชื่อเข้าใช้โดยใช้ผู้ให้บริการการตรวจสอบสิทธิ์ที่เชื่อมโยงและเข้าถึงข้อมูล Firebase เดียวกันได้
ยกเลิกการเชื่อมโยงผู้ให้บริการรับรองความถูกต้องจากบัญชีผู้ใช้
คุณสามารถยกเลิกการเชื่อมโยงผู้ให้บริการตรวจสอบสิทธิ์จากบัญชีได้ เพื่อให้ผู้ใช้ไม่สามารถลงชื่อเข้าใช้ด้วยผู้ให้บริการรายนั้นได้อีกต่อไป
หากต้องการยกเลิกการเชื่อมโยงผู้ให้บริการรับรองความถูกต้องจากบัญชีผู้ใช้ ให้ส่งรหัสผู้ให้บริการไปยังวิธี 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.AuthResult result = task.Result; Debug.LogFormat("Credentials successfully unlinked from user: {0} ({1})", result.User.DisplayName, result.User.UserId); });