สร้างผู้ใช้
คุณสร้างผู้ใช้ใหม่ในโปรเจ็กต์ Firebase โดยเรียกใช้เมธอด CreateUserWithEmailAndPassword
หรือโดยการลงชื่อเข้าใช้ผู้ใช้เป็นครั้งแรกโดยใช้ผู้ให้บริการข้อมูลประจำตัวที่ติดต่อกับภายนอก เช่น Google Sign-In หรือ Facebook Login
คุณยังสามารถสร้างผู้ใช้ที่ตรวจสอบสิทธิ์ด้วยรหัสผ่านใหม่ได้จากส่วนการตรวจสอบสิทธิ์ของ คอนโซล Firebase บนหน้าผู้ใช้
รับผู้ใช้ที่ลงชื่อเข้าใช้อยู่ในปัจจุบัน
วิธีที่แนะนำในการรับผู้ใช้ปัจจุบันคือการตั้งค่าฟังบนอ็อบเจ็กต์ Auth:
class MyAuthStateListener : public firebase::auth::AuthStateListener { public: void OnAuthStateChanged(firebase::auth::Auth* auth) override { firebase::auth::User* user = auth->current_user(); if (user != nullptr) { // User is signed in printf("OnAuthStateChanged: signed_in %s\n", user->uid().c_str()); } else { // User is signed out printf("OnAuthStateChanged: signed_out\n"); } // ... } }; // ... initialization code // Test notification on registration. MyAuthStateListener state_change_listener; auth->AddAuthStateListener(&state_change_listener);
เมื่อใช้ Listener คุณจะมั่นใจได้ว่าอ็อบเจ็กต์ Auth ไม่ได้อยู่ในสถานะระดับกลาง เช่น การเริ่มต้น เมื่อคุณได้รับผู้ใช้ปัจจุบัน
คุณยังสามารถรับผู้ใช้ที่ลงชื่อเข้าใช้อยู่ในปัจจุบันได้โดยโทรไปที่ current_user
หากผู้ใช้ไม่ได้ลงชื่อเข้าใช้ current_user
จะส่งกลับค่า nullptr
ยืนยันข้อมูลประจำตัวของผู้ใช้
ข้อมูลประจำตัวของผู้ใช้จะถูกเก็บไว้ในที่เก็บคีย์ภายในเครื่องหลังจากที่ผู้ใช้ลงชื่อเข้าใช้ แคชในเครื่องของข้อมูลประจำตัวผู้ใช้สามารถลบได้โดยการลงชื่อออกจากผู้ใช้ ที่เก็บคีย์เป็นแพลตฟอร์มเฉพาะ:
- แพลตฟอร์ม Apple: บริการพวงกุญแจ
- Android: ที่เก็บ คีย์ ของ Android
- Windows: API การจัดการข้อมูลรับรอง
- OS X: บริการพวงกุญแจ
- Linux: libsecret ซึ่งผู้ใช้ต้องติดตั้ง
รับโปรไฟล์ผู้ใช้
หากต้องการรับข้อมูลโปรไฟล์ของผู้ใช้ ให้ใช้เมธอด accessor ของอินสแตนซ์ของ firebase::auth::User
ตัวอย่างเช่น:
firebase::auth::User* user = auth->current_user(); if (user != nullptr) { std::string name = user->display_name(); std::string email = user->email(); std::string photo_url = user->photo_url(); // 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 firebase::auth::User::Token() instead. std::string uid = user->uid(); }
รับข้อมูลโปรไฟล์เฉพาะผู้ให้บริการของผู้ใช้
ในการรับข้อมูลโปรไฟล์ที่ดึงมาจากผู้ให้บริการการลงชื่อเข้าใช้ที่เชื่อมโยงกับผู้ใช้ ให้ใช้เมธอด ProviderData
ตัวอย่างเช่น:
firebase::auth::User* user = auth->current_user(); if (user != nullptr) { for (auto it = user->provider_data().begin(); it != user->provider_data().end(); ++it) { firebase::auth::UserInfoInterface* profile = *it; // Id of the provider (ex: google.com) std::string providerId = profile->provider_id(); // UID specific to the provider std::string uid = profile->uid(); // Name, email address, and profile photo Url std::string name = profile->display_name(); std::string email = profile->email(); std::string photoUrl = profile->photo_url(); } }
อัพเดทโปรไฟล์ของผู้ใช้
คุณสามารถอัปเดตข้อมูลโปรไฟล์พื้นฐานของผู้ใช้—ชื่อที่แสดงและ URL รูปโปรไฟล์—ด้วยวิธี UpdateUserProfile
ตัวอย่างเช่น:
firebase::auth::User* user = auth->current_user(); if (user != nullptr) { firebase::auth::User::UserProfile profile; profile.display_name = "Jane Q. User"; profile.photo_url = "https://example.com/jane-q-user/profile.jpg"; user->UpdateUserProfile(profile).OnCompletion( [](const firebase::Future<void>& completed_future, void* user_data) { // We are probably in a different thread right now. if (completed_future.error() == 0) { printf("User profile updated."); } }, nullptr); // pass user_data here. }
ตั้งค่าที่อยู่อีเมลของผู้ใช้
คุณสามารถตั้งค่าที่อยู่อีเมลของผู้ใช้ด้วยวิธี UpdateEmail
ตัวอย่างเช่น:
firebase::auth::User* user = auth->current_user(); if (user != nullptr) { user->UpdateEmail("user@example.com") .OnCompletion( [](const firebase::Future<void>& completed_future, void* user_data) { // We are probably in a different thread right now. if (completed_future.error() == 0) { printf("User email address updated."); } }, nullptr); }
ส่งอีเมลยืนยันให้ผู้ใช้
คุณสามารถส่งอีเมลยืนยันที่อยู่ให้กับผู้ใช้ด้วยวิธี SendEmailVerification
ตัวอย่างเช่น:
firebase::auth::User* user = auth->current_user(); if (user != nullptr) { user->SendEmailVerification().OnCompletion( [](const firebase::Future<void>& completed_future, void* user_data) { // We are probably in a different thread right now. if (completed_future.error() == 0) { printf("Email sent."); } }, nullptr); }
คุณปรับแต่งเทมเพลตอีเมลที่ใช้ในส่วนการตรวจสอบสิทธิ์ของ คอนโซล Firebase ได้ในหน้าเทมเพลตอีเมล ดู เทมเพลตอีเมล ในศูนย์ช่วยเหลือของ Firebase
ตั้งรหัสผ่านของผู้ใช้
คุณสามารถตั้งรหัสผ่านของผู้ใช้ด้วยวิธี UpdatePassword
ตัวอย่างเช่น:
firebase::auth::User* user = auth->current_user(); std::string newPassword = "SOME-SECURE-PASSWORD"; if (user != nullptr) { user->UpdatePassword(newPassword.c_str()) .OnCompletion( [](const firebase::Future<void>& completed_future, void* user_data) { // We are probably in a different thread right now. if (completed_future.error() == 0) { printf("password updated."); } }, nullptr); }
ส่งอีเมลรีเซ็ตรหัสผ่าน
คุณสามารถส่งอีเมลรีเซ็ตรหัสผ่านไปยังผู้ใช้โดยใช้เมธอด SendPasswordResetEmail
ตัวอย่างเช่น:
std::string emailAddress = "user@example.com"; auth->SendPasswordResetEmail(emailAddress.c_str()) .OnCompletion( [](const firebase::Future<void>& completed_future, void* user_data) { // We are probably in a different thread right now. if (completed_future.error() == 0) { // Email sent. } else { // An error happened. printf("Error %d: %s", completed_future.error(), completed_future.error_message()); } }, nullptr);
คุณปรับแต่งเทมเพลตอีเมลที่ใช้ในส่วนการตรวจสอบสิทธิ์ของ คอนโซล Firebase ได้ในหน้าเทมเพลตอีเมล ดู เทมเพลตอีเมล ในศูนย์ช่วยเหลือของ Firebase
คุณยังส่งอีเมลรีเซ็ตรหัสผ่านได้จากคอนโซล Firebase
ลบผู้ใช้
คุณสามารถลบบัญชีผู้ใช้ด้วยวิธี Delete
ตัวอย่างเช่น:
firebase::auth::User* user = auth->current_user(); if (user != nullptr) { user->Delete().OnCompletion( [](const firebase::Future<void>& completed_future, void* user_data) { if (completed_future.error() == 0) { // User deleted. } else { // An error happened. printf("Error %d: %s", completed_future.error(), completed_future.error_message()); } }, nullptr); }
คุณยังสามารถลบผู้ใช้ออกจากส่วนการตรวจสอบสิทธิ์ของ คอนโซล Firebase บนหน้าผู้ใช้
ตรวจสอบสิทธิ์ผู้ใช้อีกครั้ง
การดำเนินการที่มีความละเอียดอ่อนด้านความปลอดภัยบางอย่าง เช่น การลบบัญชี การตั้งค่าที่อยู่อีเมลหลัก และ การเปลี่ยนรหัสผ่าน กำหนดให้ผู้ใช้เพิ่งลงชื่อเข้าใช้ หากคุณดำเนินการอย่างใดอย่างหนึ่งเหล่านี้ และผู้ใช้ลงชื่อเข้าใช้นานเกินไป การกระทำล้มเหลว
เมื่อสิ่งนี้เกิดขึ้น ให้ตรวจสอบสิทธิ์ผู้ใช้อีกครั้งโดยรับข้อมูลรับรองการลงชื่อเข้าใช้ใหม่จากผู้ใช้และส่งข้อมูลรับรองไปยัง Reauthenticate
ตัวอย่างเช่น:
firebase::auth::User* user = auth->current_user(); // 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 != nullptr) { user->Reauthenticate(credential) .OnCompletion( [](const firebase::Future<void>& completed_future, void* user_data) { if (completed_future.error() == 0) { printf("User re-authenticated."); } }, nullptr); }
นำเข้าบัญชีผู้ใช้
คุณนำเข้าบัญชีผู้ใช้จากไฟล์ไปยังโปรเจ็กต์ Firebase ได้โดยใช้คำสั่ง auth:import
ของ Firebase CLI ตัวอย่างเช่น:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14