创建用户
您可以通过调用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