通過將Google登錄功能集成到您的應用中,您可以讓用戶使用其Google帳戶向Firebase進行身份驗證。
在你開始之前
- 將Firebase添加到您的C ++項目中。
- 在Firebase控制台中啟用Google登錄:
- 在Firebase控制台中,打開“身份驗證”部分。
- 在登錄方法標籤上,啟用Google登錄方法,然後點擊保存。
訪問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
之間存在一對一的映射。firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
使用Firebase進行身份驗證
- 按照Android和iOS上的說明獲取Google登錄的ID令牌。
- 用戶成功登錄後,將ID令牌交換為Firebase憑證,並使用Firebase憑證與Firebase進行身份驗證:
firebase::auth::Credential credential = firebase::auth::GoogleAuthProvider::GetCredential(google_id_token, nullptr); firebase::Future<firebase::auth::User*> result = auth->SignInWithCredential(credential);
- 如果您的程序有一個定期運行的更新循環(例如,每秒運行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()); } }
每次更新檢查一次結果,或者,如果您的程序是事件驅動的,則您可能希望註冊對Future的回調。
在Future上註冊回調
某些程序具有每秒被調用30或60次的Update
功能。例如,許多遊戲都遵循這種模式。這些程序可以調用LastResult
函數以輪詢異步調用。但是,如果程序是事件驅動的,則您可能希望註冊回調函數。將來完成時將調用回調函數。 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); }如果願意,回調函數也可以是lambda。
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(); }
在Firebase實時數據庫和雲存儲安全規則中,您可以從
auth
變量獲取登錄用戶的唯一用戶ID,並使用它來控制用戶可以訪問哪些數據。
通過將身份驗證提供程序憑據鏈接到現有用戶帳戶,可以允許用戶使用多個身份驗證提供程序登錄您的應用程序。
要註銷用戶,請調用SignOut()
:
auth->SignOut();