C++ 中的 Firebase 驗證入門

您可以使用 Firebase 驗證,允許使用者使用一種或多種登入方法登入您的應用程式,包括電子郵件地址和密碼登錄,以及聯合身分提供者(例如 Google 登入和 Facebook 登入)。本教學向您展示如何為您的應用程式新增電子郵件地址和密碼登錄,協助您開始使用 Firebase 身份驗證。

將您的 C++ 專案連接到 Firebase

在使用Firebase 驗證之前,您需要:

  • 註冊您的 C++ 專案並將其配置為使用 Firebase。

    如果您的 C++ 專案已使用 Firebase,則它已針對 Firebase 進行註冊和設定。

  • Firebase C++ SDK加入到您的 C++ 專案。

請注意,將 Firebase 新增至 C++ 專案涉及Firebase 控制台和開啟的 C++ 專案中的任務(例如,從控制台下載 Firebase 設定文件,然後將它們移至 C++ 專案中)。

註冊新用戶

建立一個表單,允許新使用者使用他們的電子郵件地址和密碼在您的應用程式中註冊。當使用者填寫表單時,請驗證使用者提供的電子郵件地址和密碼,然後將它們傳遞給CreateUserWithEmailAndPassword方法:

firebase::Future<firebase::auth::AuthResult> result =
    auth->CreateUserWithEmailAndPassword(email, password);

您可以透過在CreateUserWithEmailAndPasswordLastResult Future 物件上註冊回調來檢查帳戶創建操作的狀態,或者,如果您正在編寫具有某種定期更新循環的遊戲或應用程序,則可以透過輪詢更新循環中的狀態來檢查帳戶創建操作的狀態。

例如,使用 Future:

firebase::Future<firebase::auth::AuthResult> result =
    auth->CreateUserWithEmailAndPasswordLastResult();

// The lambda has the same signature as the callback function.
result.OnCompletion(
    [](const firebase::Future<firebase::auth::AuthResult>& result,
       void* user_data) {
      // `user_data` is the same as &my_program_context, below.
      // Note that we can't capture this value in the [] because std::function
      // is not supported by our minimum compiler spec (which is pre C++11).
      MyProgramContext* program_context =
          static_cast<MyProgramContext*>(user_data);

      // Process create user result...
      (void)program_context;
    },
    &my_program_context);

或者,若要使用輪詢,請在遊戲的更新循環中執行類似以下範例的操作:

firebase::Future<firebase::auth::AuthResult> result =
    auth->CreateUserWithEmailAndPasswordLastResult();
if (result.status() == firebase::kFutureStatusComplete) {
  if (result.error() == firebase::auth::kAuthErrorNone) {
    firebase::auth::AuthResult* auth_result = *result.result();
    printf("Create user succeeded for email %s\n", auth_result.user.email().c_str());
  } else {
    printf("Created user failed with error '%s'\n", result.error_message());
  }
}

登入現有用戶

建立一個表單,允許現有使用者使用其電子郵件地址和密碼登入。當使用者完成表單後,呼叫SignInWithEmailAndPassword方法:

firebase::Future<firebase::auth::AuthResult> result =
    auth->SignInWithEmailAndPassword(email, password);

取得登入操作的結果與取得註冊結果的方式相同。

設定身份驗證狀態監聽器並取得帳戶數據

若要回應登入和登出事件,請將偵聽器附加到全域身份驗證物件。每當使用者的登入狀態發生變更時,都會呼叫此偵聽器。由於偵聽器僅在身份驗證物件完全初始化後以及所有網路呼叫完成後運行,因此它是獲取有關登入使用者的資訊的最佳位置。

透過實作firebase::auth::AuthStateListener抽象類別來建立偵聽器。例如,要建立偵聽器,用於在使用者成功登入時取得有關使用者的資訊:

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());
      const std::string displayName = user.DisplayName();
      const std::string emailAddress = user.Email();
      const std::string photoUrl = user.PhotoUrl();
    } else {
      // User is signed out
      printf("OnAuthStateChanged: signed_out\n");
    }
    // ...
  }
};

使用firebase::auth::Auth物件的AddAuthStateListener方法附加偵聽器:

MyAuthStateListener state_change_listener;
auth->AddAuthStateListener(&state_change_listener);

下一步

了解如何新增對其他身分提供者和匿名訪客帳號的支援: