開始使用 C++ 中的 Firebase 驗證

您可以運用 Firebase 驗證功能,讓使用者使用 以及更多登入方式,包括電子郵件地址和密碼登入 聯合識別資訊提供者,例如 Google 登入和 Facebook 登入。這個 教學課程會介紹如何使用 Firebase 驗證功能 電子郵件地址和密碼登入應用程式。

將 C++ 專案連結至 Firebase

使用前 Firebase 驗證 請完成下列操作:

  • 註冊 C++ 專案,並將其設為使用 Firebase。

    如果您的 C++ 專案已使用 Firebase,表示該專案已註冊, 設定 Firebase 專屬的容器

  • 在 C++ 專案中新增 Firebase C++ SDK

請注意,將 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);

後續步驟

瞭解如何新增對其他識別資訊提供者和匿名訪客的支援 帳戶: