在 Firebase 中管理使用者

新增使用者

如要在 Firebase 專案中建立新使用者,請呼叫 CreateUserWithEmailAndPassword敬上 方法或透過聯合身分首次登入使用者 例如 Google 登入Facebook 登入

您也可以從「驗證」頁面建立以密碼驗證的新使用者 Firebase 控制台,找到「使用者」頁面。

取得目前登入的使用者

如要取得目前的使用者,建議您設定 驗證物件:

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);

使用事件監聽器確保 Auth 物件不在中繼中 當您取得目前使用者時的狀態,例如初始化。

您也可以呼叫 current_user,取得目前登入的使用者。如果 使用者未登入,則 is_valid 方法會傳回 false。

保留使用者憑證

使用者之後,使用者的憑證會儲存在本機 KeyStore 中 登入。簽署後,即可刪除使用者憑證本機快取 使用者。KeyStore 會因平台而異:

取得使用者的個人資料

如要取得使用者的個人資料資訊,請使用 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("[email protected]")       .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 = "[email protected]";  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 控制台,位於「使用者」頁面。

重新驗證使用者

某些涉及安全性的動作,例如 刪除帳戶 設定主要電子郵件地址,以及 變更密碼:使用者必須 最近登入。如果您執行了上述任一動作,且使用者已登入 太久,動作失敗。

在這種情況下,請取得新的登入憑證,重新驗證使用者 並將憑證傳遞至 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("[email protected]",                                                      "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 auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14