ตรวจสอบสิทธิ์โดยใช้การเข้าสู่ระบบ Facebook และ C++

จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ

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

ก่อนจะเริ่ม

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

เข้าถึง firebase::auth::Auth class

คลาส 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+ เพื่อรับโทเค็นการเข้าถึงสำหรับผู้ใช้ Facebook ที่ลงชื่อเข้าใช้
  2. หลังจากที่ผู้ใช้ลงชื่อเข้าใช้สำเร็จแล้ว ให้แลกเปลี่ยนโทเค็นการเข้าถึงสำหรับข้อมูลรับรอง Firebase และรับรองความถูกต้องกับ Firebase โดยใช้ข้อมูลรับรอง Firebase:
    firebase::auth::Credential credential =
        firebase::auth::FacebookAuthProvider::GetCredential(access_token);
    firebase::Future<firebase::auth::User*> result =
        auth->SignInWithCredential(credential);
    
  3. หากโปรแกรมของคุณมีรอบการอัพเดทที่ทำงานเป็นประจำ (พูดที่ 30 หรือ 60 ครั้งต่อวินาที) คุณสามารถตรวจสอบผลลัพธ์ได้หนึ่งครั้งต่อการอัพเดทด้วย Auth::SignInWithCredentialLastResult :
    firebase::Future<firebase::auth::User*> result =
        auth->SignInWithCredentialLastResult();
    if (result.status() == firebase::kFutureStatusComplete) {
      if (result.error() == firebase::auth::kAuthErrorNone) {
        firebase::auth::User* user = *result.result();
        printf("Sign in succeeded for `%s`\n", 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::User*> 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::User*> 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 != nullptr) {
      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 Security Rules คุณสามารถรับ ID ผู้ใช้เฉพาะของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้สามารถเข้าถึงได้

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

หากต้องการออกจากระบบผู้ใช้ โทร SignOut() :

auth->SignOut();