您可以使用 Firebase Authentication,允許使用者透過一或多種登入方式登入應用程式,包括電子郵件地址和密碼登入,以及 Google 登入和 Facebook 登入等聯合識別資訊提供者。本教學課程將介紹如何開始使用 Firebase Authentication,包括在應用程式中新增電子郵件地址和密碼登入功能。
將 C++ 專案連結至 Firebase
如要使用 Firebase Authentication,請先完成下列步驟:
註冊 C++ 專案,並設定使用 Firebase。
如果 C++ 專案已使用 Firebase,則已註冊並設定 Firebase。
將 Firebase C++ SDK 新增至 C++ 專案。
請注意,將 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);
後續步驟
瞭解如何新增其他識別資訊提供者和匿名訪客帳戶的支援功能: