您可以使用 Firebase 驗證,讓使用者透過一或多種登入方式登入應用程式,包括電子郵件地址和密碼登入,以及 Google 登入和 Facebook 登入等聯合識別資訊提供者登入。本教學課程說明如何在應用程式中新增電子郵件地址和密碼登入,以開始使用 Firebase 驗證功能。
將 C++ 專案連結至 Firebase
使用 Firebase 驗證前,您必須先完成以下事項:
註冊 C++ 專案,並將其設為使用 Firebase。
如果您的 C++ 專案已使用 Firebase,則專案已針對 Firebase 完成註冊並完成相關設定。
在 C++ 專案中新增 Firebase C++ SDK。
請注意,將 Firebase 新增至 C++ 專案時,牽涉到 Firebase 控制台和開放式 C++ 專案中的工作 (例如從控制台下載 Firebase 設定檔,再移至 C++ 專案)。
註冊新使用者
建立表單,讓新使用者能透過自己的電子郵件地址和密碼使用您的應用程式註冊。使用者填妥表單後,請驗證使用者提供的電子郵件地址和密碼,然後傳遞至 CreateUserWithEmailAndPassword
方法:
firebase::Future<firebase::auth::AuthResult> result =
auth->CreateUserWithEmailAndPassword(email, password);
如要檢查帳戶建立作業的狀態,可以在 CreateUserWithEmailAndPasswordLastResult
Future 物件上註冊回呼;或者,如果您使用某種定期更新迴圈編寫遊戲或應用程式,請在更新迴圈中輪詢狀態。
例如,使用 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::AuthResult>& 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::Future<firebase::auth::AuthResult> result =
auth->CreateUserWithEmailAndPasswordLastResult();
if (result.status() == firebase::kFutureStatusComplete) {
if (result.error() == firebase::auth::kAuthErrorNone) {
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());
}
}
登入現有使用者
建立表單,讓現有使用者能夠用自己的電子郵件地址和密碼登入。使用者填妥表單時,請呼叫 SignInWithEmailAndPassword
方法:
firebase::Future<firebase::auth::AuthResult> result =
auth->SignInWithEmailAndPassword(email, password);
取得登入作業的結果,與取得註冊結果的方式相同。
設定驗證狀態監聽器並取得帳戶資料
如要回應登入和登出事件,請將事件監聽器附加至全域驗證物件。每當使用者的登入狀態有所變更,系統就會呼叫這個事件監聽器。由於事件監聽器只會在驗證物件完全初始化後,且所有網路呼叫都完成後才會執行,因此最適合取得登入使用者的相關資訊。
實作 firebase::auth::AuthStateListener
抽象類別來建立事件監聽器。舉例來說,如要建立事件監聽器,以便在使用者成功登入時取得使用者相關資訊:
class MyAuthStateListener : public firebase::auth::AuthStateListener {
public:
void OnAuthStateChanged(firebase::auth::Auth* auth) override {
firebase::auth::User user = auth.current_user();
if (user.is_valid()) {
// User is signed in
printf("OnAuthStateChanged: signed_in %s\n", user.uid().c_str());
const std::string displayName = user.DisplayName();
const std::string emailAddress = user.Email();
const std::string photoUrl = user.PhotoUrl();
} else {
// User is signed out
printf("OnAuthStateChanged: signed_out\n");
}
// ...
}
};
使用 firebase::auth::Auth
物件的 AddAuthStateListener
方法附加事件監聽器:
MyAuthStateListener state_change_listener;
auth->AddAuthStateListener(&state_change_listener);
後續步驟
瞭解如何新增對其他識別資訊提供者和匿名訪客帳戶的支援: