ตรวจสอบสิทธิ์โดยใช้ GitHub และ C++

คุณสามารถให้ผู้ใช้ตรวจสอบสิทธิ์กับ Firebase โดยใช้บัญชี GitHub ของตนได้โดยการผสานรวมการตรวจสอบสิทธิ์ GitHub เข้ากับแอปของคุณ

ก่อนที่คุณจะเริ่มต้น

  1. เพิ่ม Firebase ในโครงการ C++ ของคุณ
  2. ใน คอนโซล Firebase ให้เปิดส่วน การรับรองความถูกต้อง
  3. บนแท็บ วิธีการลงชื่อเข้า ใช้ ให้เปิดใช้งานผู้ให้บริการ GitHub
  4. เพิ่ม รหัสไคลเอ็นต์ และ รหัสลับไคลเอ็นต์ จากคอนโซลนักพัฒนาซอฟต์แวร์ของผู้ให้บริการรายนั้นไปยังการกำหนดค่าผู้ให้บริการ:
    1. ลงทะเบียนแอปของคุณ เป็นแอปพลิเคชันสำหรับนักพัฒนาบน GitHub และรับ รหัสไคลเอ็นต์ OAuth 2.0 ของแอปและ รหัสลับไคลเอ็นต์
    2. ตรวจสอบให้แน่ใจว่า URI การเปลี่ยนเส้นทาง Firebase OAuth ของคุณ (เช่น my-app-12345.firebaseapp.com/__/auth/handler ) ถูกตั้งค่าเป็น URL โทรกลับการให้สิทธิ์ ในหน้าการตั้งค่าของแอปใน การกำหนดค่าแอป GitHub
  5. คลิก บันทึก

เข้าถึงคลาส firebase::auth::Auth

คลาส Auth เป็นเกตเวย์สำหรับการเรียก API ทั้งหมด
  1. เพิ่มไฟล์ส่วนหัวของ Auth และ App:
    #include "firebase/app.h"
    #include "firebase/auth.h"
    
  2. ในโค้ดเริ่มต้นของคุณ ให้สร้างคลาส 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__)
    
  3. รับคลาส firebase::auth::Auth สำหรับ firebase::App ของคุณ มีการแมปแบบหนึ่งต่อหนึ่งระหว่าง App และ Auth
    firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
    

รับรองความถูกต้องด้วย Firebase

  1. ทำตามคำแนะนำสำหรับ Android และ iOS+ เพื่อรับโทเค็นสำหรับผู้ใช้ GitHub ที่ลงชื่อเข้าใช้
  2. หลังจากที่ผู้ใช้ลงชื่อเข้าใช้สำเร็จ ให้แลกเปลี่ยนโทเค็นเป็นข้อมูลรับรอง Firebase และรับรองความถูกต้องกับ Firebase โดยใช้ข้อมูลรับรอง Firebase:
    firebase::auth::Credential credential =
        firebase::auth::GitHubAuthProvider::GetCredential(token);
    firebase::Future<firebase::auth::AuthResult> result =
        auth->SignInAndRetrieveDataWithCredential(credential);
    
  3. หากโปรแกรมของคุณมีลูปการอัปเดตที่ทำงานเป็นประจำ (พูดที่ 30 หรือ 60 ครั้งต่อวินาที) คุณสามารถตรวจสอบผลลัพธ์หนึ่งครั้งต่อการอัปเดตด้วย Auth::SignInAndRetrieveDataWithCredentialLastResult :
    firebase::Future<firebase::auth::AuthResult> result =
        auth->SignInAndRetrieveDataWithCredentialLastResult();
    if (result.status() == firebase::kFutureStatusComplete) {
      if (result.error() == firebase::auth::kAuthErrorNone) {
        firebase::auth::AuthResult auth_result = *result.result();
        printf("Sign in succeeded for `%s`\n",
               auth_result.user.display_name().c_str());
      } else {
        printf("Sign in failed with error '%s'\n", result.error_message());
      }
    }
    
    หรือถ้าโปรแกรมของคุณขับเคลื่อนด้วยเหตุการณ์ คุณอาจต้องการ เพื่อ ลงทะเบียนการโทรกลับในอนาคต

ลงทะเบียนการโทรกลับในอนาคต

บางโปรแกรมมีฟังก์ชัน Update ที่เรียกว่า 30 หรือ 60 ครั้งต่อวินาที ตัวอย่างเช่นหลายเกมทำตามรูปแบบนี้ โปรแกรมเหล่านี้สามารถเรียกใช้ฟังก์ชัน LastResult เพื่อสำรวจการโทรแบบอะซิงโครนัส อย่างไรก็ตาม หากโปรแกรมของคุณขับเคลื่อนด้วยเหตุการณ์ คุณอาจต้องการลงทะเบียนฟังก์ชันการโทรกลับ ฟังก์ชันการโทรกลับถูกเรียกใช้เมื่อเสร็จสิ้นอนาคต
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);
}
ฟังก์ชันการโทรกลับสามารถเป็นแลมบ์ดาได้เช่นกัน หากคุณต้องการ
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);
}

ขั้นตอนถัดไป

หลังจากที่ผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก บัญชีผู้ใช้ใหม่จะถูกสร้างขึ้นและเชื่อมโยงกับข้อมูลประจำตัว ซึ่งก็คือชื่อผู้ใช้และรหัสผ่าน หมายเลขโทรศัพท์ หรือข้อมูลผู้ให้บริการตรวจสอบสิทธิ์ ซึ่งผู้ใช้ลงชื่อเข้าใช้ด้วย บัญชีใหม่นี้จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และสามารถใช้ระบุผู้ใช้ในทุกแอปในโครงการ ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้ด้วยวิธีใดก็ตาม

  • ในแอปของคุณ คุณสามารถรับข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ได้จาก firebase::auth::User object:

    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 คุณสามารถรับ ID ผู้ใช้ที่ไม่ซ้ำกันของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้สามารถเข้าถึงได้

คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายโดย เชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่

หากต้องการลงชื่อออกจากผู้ใช้ ให้โทร SignOut() :

auth->SignOut();