在 C++ 中开始使用 Firebase Authentication

利用 Firebase 身份验证,您可以允许用户通过一种或多种登录方式(包括电子邮件地址和密码登录)以及联合身份提供方服务(如 Google 登录和 Facebook 登录)登录到您的应用。本教程将向您展示如何向您的应用添加电子邮件地址和密码登录功能,以开始使用 Firebase Authentication。

将您的 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);

后续步骤

了解如何添加对其他身份提供方和匿名访客帐号的支持: