您可以使用 Firebase 驗證讓您的使用者使用其電子郵件地址和密碼透過 Firebase 進行身份驗證,並管理您套用的基於密碼的帳戶。
在你開始之前
- 將 Firebase 新增到您的 C++ 專案。
- 如果您尚未將應用程式連線至 Firebase 項目,請從Firebase 控制台執行此操作。
- 啟用電子郵件/密碼登入:
- 在Firebase 控制台中,開啟「驗證」部分。
- 在登入方法標籤上,啟用電子郵件/密碼登入方法,然後按一下儲存。
存取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
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 即時資料庫和雲端儲存安全性規則中,您可以從
auth
變數取得登入使用者的唯一使用者 ID,並使用它來控制使用者可以存取哪些資料。
您可以透過將身分驗證提供者憑證連結到現有使用者帳戶,允許使用者使用多個驗證提供者登入您的應用程式。
若要登出用戶,請呼叫SignOut()
:
auth->SignOut();