สร้างผู้ใช้
คุณสร้างผู้ใช้ใหม่ในโปรเจ็กต์ Firebase ได้ด้วยการเรียกใช้เมธอด CreateUserWithEmailAndPassword
หรือลงชื่อเข้าใช้ผู้ใช้เป็นครั้��แรกโดยใช้ผู้ให้บริการข้อมูลประจำตัวแบบรวมศูนย์ เช่น Google Sign-In หรือการเข้าสู่ระบบ Facebook
คุณยังสร้างผู้ใช้ที่ตรวจสอบสิทธิ์ด้วยรหัสผ่านใหม่ได้จากส่วนการตรวจสอบสิทธิ์ของคอนโซล Firebase ในหน้าผู้ใช้
รับผู้ใช้ที่ลงชื่อเข้าใช้อยู่ในขณะนี้
วิธีที่แนะนำในการรับผู้ใช้ปัจจุบันคือการตั้งค่า Listener บนออบเจ็กต์การตรวจสอบสิทธิ์ ดังนี้
Firebase.Auth.FirebaseAuth auth; Firebase.Auth.FirebaseUser user; // Handle initialization of the necessary firebase modules: void InitializeFirebase() { Debug.Log("Setting up Firebase Auth"); auth = Firebase.Auth.FirebaseAuth.DefaultInstance; auth.StateChanged += AuthStateChanged; AuthStateChanged(this, null); } // Track state changes of the auth object. void AuthStateChanged(object sender, System.EventArgs eventArgs) { if (auth.CurrentUser != user) { bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null; if (!signedIn && user != null) { Debug.Log("Signed out " + user.UserId); } user = auth.CurrentUser; if (signedIn) { Debug.Log("Signed in " + user.UserId); } } } // Handle removing subscription and reference to the Auth instance. // Automatically called by a Monobehaviour after Destroy is called on it. void OnDestroy() { auth.StateChanged -= AuthStateChanged; auth = null; }
การใช้ Listener จะทำให้มั่นใจได้ว่าออบเจ็กต์การตรวจสอบสิทธิ์ไม่ได้อยู่ในสถานะตัวกลาง เช่น การเริ่มต้น เมื่อคุณได้รับผู้ใช้ปัจจุบัน
นอกจากนี้ คุณยังหาผู้ใช้ที่ลงชื่อเข้าใช้อยู่ได้โดยโทรไปที่ CurrentUser
หากผู้ใช้ไม่ได้ลงชื่อเข้าใช้ CurrentUser
จะแสดงผลเป็น Null หากผู้ใช้ออกจากระบบ IsValid()
ของผู้ใช้จะแสดงค่า "เท็จ"
ยืนยันข้อมูลประจำตัวของผู้ใช้
��ะบบจ��เ����บข้อมูลเข้าสู่ระบบของผู้ใช้ไว้ในคีย์สโตร์ในเครื่องหลังจากที่ผู้ใช้ลงชื่อเข้าใช้ คุณลบแคชข้อมูลเข้าสู่ระบบของผู้ใช้ในเครื่องได้โดยการนำผู้ใช้ออกจากระบบ Keystore มีไว้สำหรับแพลตฟอร์มโดยเฉพาะ ดังนี้
- แพลตฟอร์มของ Apple: บริการพวงกุญแจ
- Android: Android Keystore
- Windows: API การจัดการข้อมูลเข้าสู่ระบบ
- OS X: บริการพวงกุญแจ
- Linux: libsecret ที่ผู้ใช้ต้องติดตั้ง
รับโปรไฟล์ของผู้ใช้
หากต้องการข้อมูลโปรไฟล์ของผู้ใช้ ให้ใช้เมธอดผู้เข้าถึงของอินสแตนซ์ Firebase.Auth.FirebaseUser
ตัวอย่างเช่น
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { string name = user.DisplayName; string email = user.Email; System.Uri photo_url = user.PhotoUrl; // The user's Id, unique to the Firebase project. // Do NOT use this value to authenticate with your backend server, if you // have one; use User.TokenAsync() instead. string uid = user.UserId; }
รับข้อมูลโปรไฟล์เฉพาะผู้ให้บริการของผู้ใช้
หากต้องการดึงข้อมูลโปรไฟล์จากผู้ให้บริการการลงชื่อเข้าใช้ที่ลิงก์กับผู้ใช้ ให้ใช้เมธอด ProviderData
ตัวอย่างเช่น
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { foreach (var profile in user.ProviderData) { // Id of the provider (ex: google.com) string providerId = profile.ProviderId; // UID specific to the provider string uid = profile.UserId; // Name, email address, and profile photo Url string name = profile.DisplayName; string email = profile.Email; System.Uri photoUrl = profile.PhotoUrl; } }
อัปเดตโปรไฟล์ของผู้ใช้
คุณสามารถใช้เมธอด UpdateUserProfile
เพื่ออัปเดตข้อมูลโปรไฟล์ของผู้ใช้ ซึ่งได้แก่ ชื่อที่แสดงและ URL ของรูปโปรไฟล์ของผู้ใช้ ตัวอย่างเช่น
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { Firebase.Auth.UserProfile profile = new Firebase.Auth.UserProfile { DisplayName = "Jane Q. User", PhotoUrl = new System.Uri("https://example.com/jane-q-user/profile.jpg"), }; user.UpdateUserProfileAsync(profile).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UpdateUserProfileAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UpdateUserProfileAsync encountered an error: " + task.Exception); return; } Debug.Log("User profile updated successfully."); }); }
ตั้งค่าอีเมลของผู้ใช้
คุณจะตั้งค่าอีเมลของผู้ใช้ได้โดยใช้เมธอด UpdateEmail
ตัวอย่างเช่น
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { user.UpdateEmailAsync("user@example.com").ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UpdateEmailAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UpdateEmailAsync encountered an error: " + task.Exception); return; } Debug.Log("User email updated successfully."); }); }
ส่งอีเมลยืนยันให้ผู้ใช้
คุณส่งอีเมลยืนยันที่อยู่ให้กับผู้ใช้ได้ด้วยเมธอด SendEmailVerification
ตัวอย่างเช่น
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { user.SendEmailVerificationAsync().ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SendEmailVerificationAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SendEmailVerificationAsync encountered an error: " + task.Exception); return; } Debug.Log("Email sent successfully."); }); }
คุณปรับแต่งเทมเพลตอีเมลที่ใช้ในส่วนการตรวจสอบสิทธิ์ของคอนโซล Firebase ได้ในหน้าเทมเพลตอีเมล ดูเทมเพลตอีเมลใน ศูนย์ช่วยเหลือ Firebase
ตั้งรหัสผ่านของผู้ใช้
คุณตั้งรหัสผ่านของผู้ใช้ได้โดยใช้เมธอด UpdatePassword
ตัวอย่างเช่น
Firebase.Auth.FirebaseUser user = auth.CurrentUser; string newPassword = "SOME-SECURE-PASSWORD"; if (user != null) { user.UpdatePasswordAsync(newPassword).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("UpdatePasswordAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("UpdatePasswordAsync encountered an error: " + task.Exception); return; } Debug.Log("Password updated successfully."); }); }
ส่งอีเมลรีเซ็ตรหัสผ่าน
คุณส่งอีเมลการรีเซ็ตรหัสผ่านไปยังผู้ใช้ได้โดยใช้เมธอด SendPasswordResetEmail
ตัวอย่างเช่น
string emailAddress = "user@example.com"; if (user != null) { auth.SendPasswordResetEmailAsync(emailAddress).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SendPasswordResetEmailAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SendPasswordResetEmailAsync encountered an error: " + task.Exception); return; } Debug.Log("Password reset email sent successfully."); }); }
คุณปรับแต่งเทมเพลตอีเมลที่ใช้ในส่วนการตรวจสอบสิทธิ์ของคอนโซล Firebase ได้ในหน้าเทมเพลตอีเมล ดูเทมเพลตอีเมลใน ศูนย์ช่วยเหลือ Firebase
คุณส่งอีเมลรีเซ็ตรหัสผ่านจากคอนโซล Firebase ได้ด้วย
ลบผู้ใช้
คุณลบบัญชีผู้ใช้ได้ด้วยเมธอด Delete
ตัวอย่างเช่น
Firebase.Auth.FirebaseUser user = auth.CurrentUser; if (user != null) { user.DeleteAsync().ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("DeleteAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("DeleteAsync encountered an error: " + task.Exception); return; } Debug.Log("User deleted successfully."); }); }
คุณยังลบผู้ใช้จากส่วนการตรวจสอบสิทธิ์ของคอนโซล Firebase ในหน้าผู้ใช้ได้ด้วย
ตรวจสอบสิทธิ์ผู้ใช้อีกครั้ง
ผู้ใช้เพิ่งลงชื่อเข้าใช้เพื่อดำเนินการที่มีความละเอียดอ่อนด้านความปลอดภัย เช่น การลบบัญชี การตั้งค่าที่อยู่อีเมลหลัก และการเปลี่ยนรหัสผ่าน หากคุณดำเนินการอย่างใดอย่างหนึ่งข้างต้นและผู้ใช้���งชื่อเข้าใช้นานเกินไป การดำเนินการจะไม่สำเร็จ
ในกรณีนี้ ให้ตรวจสอบสิทธิ์ผู้ใช้อีกครั้งโดยรับข้อมูลเข้าสู่ระบบใหม่สำหรับลงชื่อเข้าใช้จากผู้ใช้ แล้วส่งข้อมูลเข้าสู่ระบบไปให้ Reauthenticate
ตัวอย่างเช่น
Firebase.Auth.FirebaseUser user = auth.CurrentUser; // Get auth credentials from the user for re-authentication. The example below shows // email and password credentials but there are multiple possible providers, // such as GoogleAuthProvider or FacebookAuthProvider. Firebase.Auth.Credential credential = Firebase.Auth.EmailAuthProvider.GetCredential("user@example.com", "password1234"); if (user != null) { user.ReauthenticateAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("ReauthenticateAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("ReauthenticateAsync encountered an error: " + task.Exception); return; } Debug.Log("User reauthenticated successfully."); }); }
นำเข้าบัญชีผู้ใช้
คุณนำเข้าบัญชีผู้ใช้จากไฟล์ไปยังโปรเจ็กต์ Firebase ได้โดยใช้คำสั่ง auth:import
ของ Firebase CLI ตัวอย่างเช่น
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14