Firebase Authenticationを使用すると、ユーザーは、電子メールアドレスやパスワードのサインイン、GoogleサインインやFacebookログインなどのフェデレーションIDプロバイダーなど、1つ以上のサインイン方法を使用してアプリにサインインできます。このチュートリアルでは、アプリにメールアドレスとパスワードのログインを追加する方法を紹介することで、FirebaseAuthenticationの使用を開始します。
C ++プロジェクトをFirebaseに接続します
Firebase認証を使用する前に、次のことを行う必要があります。
C ++プロジェクトを登録し、Firebaseを使用するように構成します。
C ++プロジェクトがすでにFirebaseを使用している場合は、Firebase用に登録および構成されています。
Firebase C ++ SDKをC ++プロジェクトに追加します。
FirebaseをC ++プロジェクトに追加するには、 Firebaseコンソールと開いているC ++プロジェクトの両方でタスクが必要になることに注意してください(たとえば、コンソールからFirebase構成ファイルをダウンロードしてからC ++プロジェクトに移動します)。
新規ユーザーを登録する
新規ユーザーがメールアドレスとパスワードを使用してアプリに登録できるフォームを作成します。ユーザーがフォームに入力したら、ユーザーから提供された電子メールアドレスとパスワードを検証し、それらをCreateUserWithEmailAndPassword
メソッドに渡します。
firebase::Future<firebase::auth::User*> result =
auth->CreateUserWithEmailAndPassword(email, password);
アカウント作成操作のステータスは、 CreateUserWithEmailAndPasswordLastResult
Futureオブジェクトにコールバックを登録するか、ある種の定期的な更新ループを使用してゲームやアプリを作成している場合は、更新ループでステータスをポーリングすることで確認できます。
たとえば、Futureを使用します。
firebase::Future<firebase::auth::User*> result =
auth->CreateUserWithEmailAndPasswordLastResult();
// The lambda has the same signature as the callback function.
result.OnCompletion(
[](const firebase::Future<firebase::auth::User*>& 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::User*> result =
auth->CreateUserWithEmailAndPasswordLastResult();
if (result.status() == firebase::kFutureStatusComplete) {
if (result.error() == firebase::auth::kAuthErrorNone) {
firebase::auth::User* user = *result.result();
printf("Create user succeeded for email %s\n", user->email().c_str());
} else {
printf("Created user failed with error '%s'\n", result.error_message());
}
}
既存のユーザーにサインインする
既存のユーザーが自分のメールアドレスとパスワードを使用してサインインできるフォームを作成します。ユーザーがフォームに入力したら、 SignInWithEmailAndPassword
メソッドを呼び出します。
firebase::Future<firebase::auth::User*> 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 != nullptr) {
// 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 firebase::auth::Auth
オブジェクトのAddAuthStateListener
メソッドにアタッチします。
MyAuthStateListener state_change_listener;
auth->AddAuthStateListener(&state_change_listener);
次のステップ
他のIDプロバイダーと匿名ゲストアカウントのサポートを追加する方法を学びます。