GitHub と C++ を使用して認証する (original) (raw)

GitHub 認証をアプリに統合することで、ユーザーは GitHub アカウントを使用して Firebase での認証を行えます。

始める前に

  1. Firebase を C++ プロジェクトに追加します
  2. Firebase コンソールで [Auth] セクションを開きます。
  3. [Sign-in method] タブで、[GitHub] プロバイダを有効にします。
  4. そのプロバイダのデベロッパー コンソールで取得したクライアント IDクライアント シークレットをプロバイダ構成に追加します。
    1. GitHub でデベロッパー アプリケーションとしてアプリを登録し、アプリの OAuth 2.0 クライアント IDクライアント シークレットを取得します。
    2. GitHub アプリの構成にあるアプリ設定ページで、Firebase OAuth リダイレクト URImy-app-12345.firebaseapp.com/__/auth/handler など)を認可コールバック URL として設定します。
  5. [保存] をクリックします。

firebase::auth::Auth クラスへのアクセス

すべての API 呼び出しは Auth クラスを使用して行われます。

  1. Auth ヘッダー ファイルと App ヘッダー ファイルを追加します。

#include "firebase/app.h"
#include "firebase/auth.h" 2. 初期化コードで firebase::App クラスを作成します。
#if defined(ANDROID)
firebase::App* app =
firebase::App::Create(firebase::AppOptions(), my_jni_env, my_activity);
#else
firebase::App* app = firebase::App::Create(firebase::AppOptions());
#endif // defined(ANDROID) 3. firebase::Appfirebase::auth::Auth クラスを取得します。AppAuth は、1 対 1 で対応しています。
firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);

Firebase で認証する

  1. Android 用または iOS+ 用の手順に沿って、ログイン済み GitHub ユーザーのトークンを取得します。
  2. ユーザーがログインに成功したら、トークンを Firebase 認証情報と交換し、Firebase 認証情報を使用して Firebase での認証を行います。
    firebase::auth::Credential credential =
    firebase::auth::GitHubAuthProvider::GetCredential(token);
    firebase::Futurefirebase::auth::AuthResult result =
    auth->SignInAndRetrieveDataWithCredential(credential);
  3. 定期的に(たとえば、毎秒 30 回または 60 回)実行される更新ループがプログラムに含まれている場合、Auth::SignInAndRetrieveDataWithCredentialLastResult を使用して、更新されるたびに 1 回結果を確認できます。
    firebase::Futurefirebase::auth::AuthResult result =
    auth->SignInAndRetrieveDataWithCredentialLastResult();
    if (result.status() == firebase::kFutureStatusComplete) {
    if (result.error() == firebase::auth::kAuthErrorNone) {
    firebase::auth::AuthResult auth_result = *result.result();
    printf("Sign in succeeded for %s\n",
    auth_result.user.display_name().c_str());

} else {
printf("Sign in failed with error '%s'\n", result.error_message());
}
}
プログラムがイベント ドリブンの場合は、Future にコールバックを登録することをおすすめします。

Future にコールバックを登録する

プログラムの中には、毎秒 30 回または 60 回呼び出される Update 関数が含まれるものがあります。たとえば、多くのゲームでこのモデルが使用されています。このようなプログラムでは、LastResult 関数を呼び出して、非同期呼び出しをポーリングできます。ただし、プログラムがイベント ドリブンの場合は、コールバック関数を登録することをおすすめします。コールバック関数は、Future の完了時に呼び出されます。

void OnCreateCallback(const firebase::Futurefirebase::auth::User*& result, void* user_data) { // The callback is called when the Future enters the complete state. assert(result.status() == firebase::kFutureStatusComplete);

// Use user_data to pass-in program context, if you like. MyProgramContext* program_context = static_cast<MyProgramContext*>(user_data);

// Important to handle both success and failure situations. if (result.error() == firebase::auth::kAuthErrorNone) { firebase::auth::User* user = *result.result(); printf("Create user succeeded for email %s\n", user->email().c_str());

// Perform other actions on User, if you like.
firebase::auth::User::UserProfile profile;
profile.display_name = program_context->display_name;
user->UpdateUserProfile(profile);

} else { printf("Created user failed with error '%s'\n", result.error_message()); } }

void CreateUser(firebase::auth::Auth* auth) { // Callbacks work the same for any firebase::Future. firebase::Futurefirebase::auth::AuthResult result = auth->CreateUserWithEmailAndPasswordLastResult();

// &my_program_context is passed verbatim to OnCreateCallback(). result.OnCompletion(OnCreateCallback, &my_program_context); }

コールバック関数にラムダを使用することもできます。

void CreateUserUsingLambda(firebase::auth::Auth* auth) { // Callbacks work the same for any firebase::Future. firebase::Futurefirebase::auth::AuthResult result = auth->CreateUserWithEmailAndPasswordLastResult();

// The lambda has the same signature as the callback function. result.OnCompletion( [](const firebase::Futurefirebase::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 プロジェクトの一部として保存され、ユーザーのログイン方法にかかわらず、プロジェクトのすべてのアプリでユーザーを識別するために使用できます。

既存のユーザー アカウントに認証プロバイダの認証情報をリンクすることで、ユーザーは複数の認証プロバイダを使用してアプリにログインできるようになります。

ユーザーのログアウトを行うには、SignOut() を呼び出します。

auth->SignOut();