新增使用者
您可以呼叫 CreateUserWithEmailAndPassword
方法,或使用聯合識別資訊提供者 (例如 Google 登入或 Facebook 登入),在 Firebase 專案中建立新使用者。
您也可以透過 Firebase 控制台的「Users」(使用者) 頁面,從「Authentication」(驗證) 部分建立新的以密碼驗證的使用者。
取得目前登入的使用者
如要取得目前的使用者,建議您在 Auth 物件上設定事件監聽器:
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 會因平台而異:
- Apple 平台:Keychain Services。
- Android:Android KeyStore。
- Windows:Credential Management API。
- OS X:Keychain Services。
- 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 控制台的「使用者」頁面,從「驗證」專區刪除使用者。
重新驗證使用者
某些攸關安全的動作 (例如刪除帳戶、設定主要電子郵件地址,以及變更密碼) 使用者必須最近才登入,如果您執行上述其中一項操作,但使用者登入的時間太長,動作就會失敗。
在這種情況下,請從使用者取得新的登入憑證,並將憑證傳遞至 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