您可以使用 Firebase 身份驗證讓您的用戶使用他們的電子郵件地址和密碼通過 Firebase 進行身份驗證,並管理您應用的基於密碼的帳戶。
在你開始之前
- 將 Firebase 添加到您的 C++ 項目。
- 如果您尚未將您的應用程序連接到您的 Firebase 項目,請從Firebase 控制台執行此操作。
- 啟用電子郵件/密碼登錄:
- 在Firebase 控制台中,打開Auth部分。
- 在登錄方法選項卡上,啟用電子郵件/密碼登錄方法並單擊保存。
訪問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);
創建基於密碼的帳戶
要創建一個帶密碼的新用戶帳戶,請在您應用的登錄代碼中完成以下步驟:
- 當新用戶使用您應用的註冊表單註冊時,請完成您的應用所需的所有新帳戶驗證步驟,例如驗證新帳戶的密碼是否輸入正確並滿足您的複雜性要求。
- 通過將新用戶的電子郵件地址和密碼傳遞給
Auth::CreateUserWithEmailAndPassword
來創建一個新帳戶:firebase::Future<firebase::auth::AuthResult> result = auth->CreateUserWithEmailAndPassword(email, password);
- 如果你的程序有一個定期運行的更新循環(比如每秒 30 或 60 次),你可以使用
Auth::CreateUserWithEmailAndPasswordLastResult
檢查一次結果:firebase::Future<firebase::auth::AuthResult> result = auth->CreateUserWithEmailAndPasswordLastResult(); if (result.status() == firebase::kFutureStatusComplete) { if (result.error() == firebase::auth::kAuthErrorNone) { const 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()); } }
或者,如果你的程序是事件驅動的,你可能更喜歡在 Future 上註冊回調。
使用電子郵件地址和密碼登錄用戶
使用密碼登錄用戶的步驟與創建新帳戶的步驟類似。在您應用的登錄功能中,執行以下操作:
- 當用戶登錄您的應用時,將用戶的電子郵件地址和密碼傳遞給
firebase::auth::Auth::SignInWithEmailAndPassword
:firebase::Future<firebase::auth::AuthResult> result = auth->SignInWithEmailAndPassword(email, password);
- 如果你的程序有一個定期運行的更新循環(比如每秒 30 或 60 次),你可以使用
Auth::SignInWithEmailAndPasswordLastResult
檢查一次結果:firebase::Future<firebase::auth::AuthResult> result = auth->SignInWithEmailAndPasswordLastResult(); if (result.status() == firebase::kFutureStatusComplete) { if (result.error() == firebase::auth::kAuthErrorNone) { const firebase::auth::AuthResult auth_result = *result.result(); printf("Sign in succeeded for email %s\n", auth_result.user.email().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::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::AuthResult> 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::AuthResult> 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); }
推薦:啟用電子郵件枚舉保護
如果電子郵件地址在必須註冊時未註冊(例如,使用電子郵件地址和密碼登錄時),或者在必須不使用時註冊(例如,更改用戶的電子郵件地址時)。雖然這有助於向用戶建議具體的補救措施,但它也可能被惡意行為者濫用以發現用戶註冊的電子郵件地址。
為降低此風險,我們建議您使用 Google Cloud gcloud
工具為您的項目啟用電子郵件枚舉保護。請注意,啟用此功能會更改 Firebase 身份驗證的錯誤報告行為:確保您的應用不依賴於更具體的錯誤。
下一步
用戶首次登錄後,將創建一個新的用戶帳戶並將其鏈接到用戶登錄所用的憑據,即用戶名和密碼、電話號碼或身份驗證提供商信息。這個新帳戶存儲為您的 Firebase 項目的一部分,可用於在項目中的每個應用程序中識別用戶,無論用戶如何登錄。
在您的應用中,您可以從
firebase::auth::User
對象獲取用戶的基本個人資料信息:firebase::auth::User user = auth->current_user(); if (user.is_valid()) { 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 Realtime Database 和 Cloud Storage Security Rules中,您可以從
auth
變量中獲取登錄用戶的唯一用戶 ID,並使用它來控制用戶可以訪問的數據。
您可以允許用戶使用多個身份驗證提供程序登錄您的應用程序,方法是將身份驗證提供程序憑據鏈接到現有用戶帳戶。
要註銷用戶,請調用SignOut()
:
auth->SignOut();