สร้างผู้ใช้
คุณสร้างผู้ใช้ใหม่ในโปรเจ็กต์ Firebase ของคุณโดยการเรียกใช้เมธอด CreateUserWithEmailAndPassword
หรือโดยการลงชื่อเข้าใช้ผู้ใช้เป็นครั้งแรกโดยใช้ผู้ให้บริการข้อมูลประจำตัวแบบรวมศูนย์ เช่น การลงชื่อเข้าใช้ Google หรือ การเข้าสู่ระบบด้วย Facebook
คุณยังสามารถสร้างผู้ใช้ที่ตรวจสอบรหัสผ่านใหม่ได้จากส่วนการตรวจสอบสิทธิ์ของ คอนโซล Firebase ในหน้าผู้ใช้
รับผู้ใช้ที่ลงชื่อเข้าใช้อยู่ในปัจจุบัน
วิธีที่แนะนำในการรับผู้ใช้ปัจจุบันคือการตั้งค่า Listener บนออบเจ็กต์ Auth:
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 ช่วยให้มั่นใจได้ว่าออบเจ็กต์ Auth ไม่อยู่ในสถานะกลาง เช่น การเริ่มต้น เมื่อคุณได้รับผู้ใช้ปัจจุบัน
คุณยังสามารถรับผู้ใช้ที่ลงชื่อเข้าใช้อยู่ในปัจจุบันได้โดยโทร CurrentUser
หากผู้ใช้ไม่ได้ลงชื่อเข้าใช้ CurrentUser
จะส่งคืนค่าว่าง หากผู้ใช้ออกจาก IsValid()
ของผู้ใช้จะคืนค่าเท็จ
ยืนยันข้อมูลรับรองของผู้ใช้
ข้อมูลรับรองของผู้ใช้จะถูกจัดเก็บไว้ในที่เก็บคีย์ในเครื่องหลังจากที่ผู้ใช้ลงชื่อเข้าใช้ แคชในเครื่องของหนังสือรับรองผู้ใช้สามารถลบได้โดยการออกจากระบบผู้ใช้ ที่เก็บคีย์เป็นแพลตฟอร์มเฉพาะ:
- แพลตฟอร์มของ Apple: บริการพวงกุญแจ
- Android: ที่เก็บคีย์ Android
- 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; } }
อัปเดตโปรไฟล์ของผู้ใช้
คุณสามารถอัปเดตข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ เช่น ชื่อที่แสดงและ URL รูปโปรไฟล์ของผู้ใช้ได้ด้วยวิธี UpdateUserProfile
ตัวอย่างเช่น:
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