新增使用者
您可以透過下列方式建立新使用者:
從應用程式:呼叫
CreateUserWithEmailAndPassword方法,在 Firebase 專案中建立新使用者,或是使用聯合身分識別資訊提供者 (例如 Google 登入或 Facebook 登入) 首次登入使用者。在 Firebase 控制台中:在「Security」(安全性) >「Authentication」(驗證) >「Users」(使用者) 分頁中,建立新的密碼驗證使用者。
取得目前登入的使用者
建議您在驗證物件上設定事件監聽器,藉此取得目前使用者:
class MyAuthStateListener : public firebase::auth::AuthStateListener { public: void OnAuthStateChanged(firebase::auth::Auth* auth) override { firebase::auth::User user = auth->current_user(); if (user.is_valid()) { // 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);
使用事件監聽器可確保您取得目前使用者時,驗證物件不會處於中繼狀態 (例如初始化)。
您也可以呼叫 current_user,取得目前登入的使用者。如果使用者未登入,使用者的 is_valid 方法會傳回 false。
保存使用者憑證
使用者登入後,系統會將憑證儲存在本機金鑰儲存區。登出使用者即可刪除本機快取的使用者憑證。金鑰儲存區會因平台而異:
- Apple 平台:鑰匙圈服務。
- Android:Android Keystore。
- Windows:憑證管理 API。
- OS X:鑰匙圈服務。
- Linux:libsecret,使用者必須安裝這個程式庫。
取得使用者個人資料
如要取得使用者的個人資料資訊,請使用 firebase::auth::User 執行個體的存取子方法。例如:
firebase::auth::User user = auth->current_user(); if (user.is_valid()) { 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.is_valid()) { 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 方法,更新使用者的基本個人資料資訊,包括顯示名稱和個人資料相片網址。例如:
firebase::auth::User user = auth->current_user(); if (user.is_valid()) { 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.is_valid()) { 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.is_valid()) { 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.is_valid()) { 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.is_valid()) { 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 控制台中依序點選「Security」(安全性) >「Authentication」(驗證) >「Users」(使用者) 分頁標籤,刪除使用者。
重新驗證使用者
執行部分安全性敏感操作 (例如刪除帳戶、設定主要電子郵件地址和變更密碼) 時,使用者必須最近登入帳戶。如果您執行上述任一動作,但使用者登入時間已過長,動作就會失敗。
發生這種情況時,請向使用者索取新的登入憑證,並將憑證傳遞至 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.is_valid()) { 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