創建用戶
您可以通過調用CreateUserWithEmailAndPassword
方法或通過使用聯合身份提供商(例如Google Sign-In或Facebook Login )首次登錄用戶來在 Firebase 項目中創建一個新用戶。
您還可以在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);
通過使用偵聽器,您可以確保 Auth 對像在獲取當前用戶時不處於中間狀態(例如初始化)。
您還可以通過調用current_user
來獲取當前登錄的用戶。如果用戶未登錄, current_user
返回 nullptr。
保留用戶的憑據
用戶登錄後,用戶的憑據將存儲在本地密鑰庫中。用戶憑據的本地緩存可以通過註銷用戶來刪除。密鑰庫是特定於平台的:
獲取用戶的個人資料
要獲取用戶的個人資料信息,請使用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(); } }
更新用戶的個人資料
您可以使用UpdateUserProfile
方法更新用戶的基本個人資料信息(用戶的顯示名稱和個人資料照片 URL)。例如:
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 CLI 的auth:import
命令將用戶帳戶從文件導入您的 Firebase 項目。例如:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14