Twitter認証をアプリに統合することで、ユーザーがTwitterアカウントを使用してFirebaseで認証できるようにすることができます。
あなたが始める前に
- C ++プロジェクトにFirebaseを追加します。
- Firebaseコンソールで、[認証]セクションを開きます。
- [サインイン方法]タブで、 Twitterプロバイダーを有効にします。
- そのプロバイダーの開発者コンソールからプロバイダー構成にAPIキーとAPIシークレットを追加します。
- アプリをTwitterで開発者アプリケーションとして登録し、アプリのOAuthAPIキーとAPIシークレットを取得します。
- Firebase OAuthリダイレクトURI (例:
my-app-12345.firebaseapp.com/__/auth/handler
)が、 Twitterアプリの構成のアプリの設定ページで承認コールバックURLとして設定されていることを確認します。
- [保存]をクリックします。
firebase::auth::Auth
クラスにアクセスする
Auth
クラスは、すべてのAPI呼び出しのゲートウェイです。- AuthおよびAppヘッダーファイルを追加します:
#include "firebase/app.h" #include "firebase/auth.h"
- 初期化コードで、
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__)
-
firebase::App
のfirebase::auth::Auth
クラスを取得します。App
とAuth
の間には1対1のマッピングがあります。firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
Firebaseで認証する
- サインインとTwitterのドキュメントに従って、OAuthアクセストークンとOAuthシークレットを取得します。
- ユーザーが正常にサインインした後、トークンとシークレットをFirebaseクレデンシャルと交換し、Firebaseクレデンシャルを使用してFirebaseで認証します:
firebase::auth::Credential credential = firebase::auth::TwitterAuthProvider::GetCredential(token, secret); firebase::Future<firebase::auth::User*> result = auth->SignInWithCredential(credential);
- プログラムに定期的に実行される更新ループがある場合(たとえば、1秒間に30回または60回)、
Auth::SignInWithCredentialLastResult
:firebase::Future<firebase::auth::User*> result = auth->SignInWithCredentialLastResult(); if (result.status() == firebase::kFutureStatusComplete) { if (result.error() == firebase::auth::kAuthErrorNone) { firebase::auth::User* user = *result.result(); printf("Sign in succeeded for `%s`\n", user->display_name().c_str()); } else { printf("Sign in failed with error '%s'\n", result.error_message()); } }
を使用して、更新ごとに1回結果を確認できます。または、プログラムがイベント駆動型の場合は、 Futureにコールバックを登録します。
Futureにコールバックを登録する
一部のプログラムには、1秒間に30回または60回呼び出されるUpdate
関数があります。たとえば、多くのゲームはこのモデルに従います。これらのプログラムは、 LastResult
関数を呼び出して非同期呼び出しをポーリングできます。ただし、プログラムがイベント駆動型の場合は、コールバック関数を登録することをお勧めします。 Futureが完了すると、コールバック関数が呼び出されます。void OnCreateCallback(const firebase::Future<firebase::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::Future<firebase::auth::User*> 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::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プロジェクトの一部として保存され、ユーザーのログイン方法に関係なく、プロジェクト内のすべてのアプリでユーザーを識別するために使用できます。
アプリでは、
firebase::auth::User
オブジェクトからユーザーの基本的なプロファイル情報を取得できます。firebase::auth::User* user = auth->current_user(); if (user != nullptr) { std::string name = user->display_name(); std::string email = user->email(); std::string photo_url = user->photo_url(); // The user's ID, unique to the Firebase project. // Do NOT use this value to authenticate with your backend server, // if you have one. Use firebase::auth::User::Token() instead. std::string uid = user->uid(); }
FirebaseRealtimeデータベースとCloudStorageのセキュリティルールでは、ログインしたユーザーの一意のユーザーIDを
auth
変数から取得し、それを使用してユーザーがアクセスできるデータを制御できます。
認証プロバイダーのクレデンシャルを既存のユーザーアカウントにリンクすることで、ユーザーが複数の認証プロバイダーを使用してアプリにサインインできるようにすることができます。
ユーザーをサインアウトするには、 SignOut()
を呼び出します。
auth->SignOut();