คุณใช้ Firebase Authentication เพื่ออนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปได้โดยใช้ วิธีการลงชื่อเข้าใช้อื่นๆ ซึ่งรวมถึงการลงชื่อเข้าใช้ด้วยอีเมลและรหัสผ่าน และ ผู้ให้บริการข้อมูลประจำตัวแบบรวมศูนย์ เช่น Google Sign-In และ 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);
คุณสามารถตรวจสอบสถานะการดำเนินการสร้างบัญชีได้โดยการลงทะเบียน
Callback ในออบเจ็กต์ CreateUserWithEmailAndPasswordLastResult 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);
ได้รับผลของการลงชื่อเข้าใช้ในลักษณะเดียวกับที่คุณได้รับผลการลงชื่อสมัครใช้
ตั้งค่า Listener สถานะการตรวจสอบสิทธิ์และรับข้อมูลบัญชี
แนบ Listener ส่วนกลางเพื่อตอบกลับเหตุการณ์การลงชื่อเข้าใช้และออกจากระบบ ออบเจ็กต์การตรวจสอบสิทธิ์ ระบบจะเรียก Listener นี้เมื่อใดก็ตามที่ผู้ใช้ลงชื่อเข้าใช้ การเปลี่ยนแปลงสถานะ เนื่องจาก Listener จะทำงานหลังจากออบเจ็กต์การตรวจสอบสิทธิ์แล้วเท่านั้น เริ่มต้นอย่างสมบูรณ์ และหลังจากที่การเรียกเครือข่ายเสร็จสมบูรณ์ ตำแหน่งสำหรับดูข้อมูลเกี่ยวกับผู้ใช้ที่ลงชื่อเข้าใช้
สร้าง Listener ด้วยการใช้ firebase::auth::AuthStateListener
Abstract Class เช่น ในการสร้าง Listener ที่ได้รับข้อมูลเกี่ยวกับ
ผู้ใช้เมื่อผู้ใช้ลงชื่อเข้าใช้สำเร็จ
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");
    }
    // ...
  }
};
แนบ Listener กับออบเจ็กต์ firebase::auth::Auth
เมธอด AddAuthStateListener:
MyAuthStateListener state_change_listener;
auth->AddAuthStateListener(&state_change_listener);
ขั้นตอนถัดไป
ดูวิธีเพิ่มการรองรับผู้ให้บริการข้อมูลประจำตัวรายอื่นๆ และผู้มาเยือนที่ไม่ระบุชื่อ บัญชี: