จัดการผู้ใช้ใน Firebase

สร้างผู้ใช้

คุณมีตัวเลือกต่อไปนี้ในการสร้างผู้ใช้ใหม่

  • จากแอป: สร้างผู้ใช้ใหม่ในโปรเจ็กต์ Firebase โดยเรียกใช้ CreateUserWithEmailAndPassword เมธอด หรือลงชื่อเข้าใช้ผู้ใช้เป็นครั้งแรกโดยใช้ผู้ให้บริการข้อมูลประจำตัวแบบรวม เช่น Google Sign-In หรือ การเข้าสู่ระบบด้วย Facebook

  • ในคอนโซลFirebase : สร้างผู้ใช้ที่ตรวจสอบสิทธิ์ด้วยรหัสผ่านใหม่ ในแท็บความปลอดภัย > การตรวจสอบสิทธิ์ > ผู้ใช้

รับผู้ใช้ที่ลงชื่อเข้าใช้ในปัจจุบัน

วิธีที่แนะนำในการรับผู้ใช้ปัจจุบันคือการตั้งค่า Listener ในออบเจ็กต์ Auth ดังนี้

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());
    } else {
      // User is signed out
      printf("OnAuthStateChanged: signed_out\n");
    }
    // ...
  }
};
// ... initialization code
// Test notification on registration.
MyAuthStateListener state_change_listener;
auth->AddAuthStateListener(&state_change_listener);

การใช้ Listener จะช่วยให้มั่นใจได้ว่าออบเจ็กต์การตรวจสอบสิทธิ์จะไม่อยู่ในสถานะกลาง เช่น การเริ่มต้น เมื่อคุณรับผู้ใช้ปัจจุบัน

นอกจากนี้ คุณยังรับผู้ใช้ที่ลงชื่อเข้าใช้ในปัจจุบันได้โดยเรียกใช้ current_user หากผู้ใช้ไม่ได้ลงชื่อเข้าใช้ เมธอด is_valid ของผู้ใช้จะแสดงผลเป็นเท็จ

เก็บข้อมูลเข้าสู่ระบบของผู้ใช้ไว้

ระบบจะจัดเก็บข้อมูลเข้าสู่ระบบของผู้ใช้ไว้ใน Keystore ในเครื่องหลังจากที่ผู้ใช้ลงชื่อเข้าใช้ คุณสามารถลบแคชข้อมูลเข้าสู่ระบบของผู้ใช้ในเครื่องได้โดยออกจากระบบของผู้ใช้ Keystore จะขึ้นอยู่กับแพลตฟอร์ม

ดูโปรไฟล์ของผู้ใช้

หากต้องการดูข้อมูลโปรไฟล์ของผู้ใช้ ให้ใช้เมธอดตัวเข้าถึงของอินสแตนซ์ firebase::auth::User เช่น

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();
}

ดูข้อมูลโปรไฟล์ของผู้ให้บริการ

หากต้องการดูข้อมูลโปรไฟล์ที่ดึงมาจากผู้ให้บริการลงชื่อเข้าใช้ที่ลิงก์กับผู้ใช้ ให้ใช้เมธอด ProviderData เช่น

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  for (auto it = user.provider_data().begin();
       it != user.provider_data().end(); ++it) {
    firebase::auth::UserInfoInterface profile = *it;
    // Id of the provider (ex: google.com)
    std::string providerId = profile.provider_id();

    // UID specific to the provider
    std::string uid = profile.uid();

    // Name, email address, and profile photo Url
    std::string name = profile.display_name();
    std::string email = profile.email();
    std::string photoUrl = profile.photo_url();
  }
}

อัปเดตโปรไฟล์ของผู้ใช้

คุณสามารถอัปเดตข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ ซึ่งได้แก่ ชื่อที่แสดงและ URL รูปโปรไฟล์ ด้วยเมธอด UpdateUserProfile เช่น

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  firebase::auth::User::UserProfile profile;
  profile.display_name = "Jane Q. User";
  profile.photo_url = "https://example.com/jane-q-user/profile.jpg";
  user.UpdateUserProfile(profile).OnCompletion(
      [](const firebase::Future<void>& completed_future, void* user_data) {
        // We are probably in a different thread right now.
        if (completed_future.error() == 0) {
          printf("User profile updated.");
        }
      },
      nullptr);  // pass user_data here.
}

ตั้งค่าอีเมลของผู้ใช้

คุณสามารถตั้งค่าอีเมลของผู้ใช้ด้วยเมธอด UpdateEmail เช่น

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  user.UpdateEmail("user@example.com")
      .OnCompletion(
          [](const firebase::Future<void>& completed_future,
             void* user_data) {
            // We are probably in a different thread right now.
            if (completed_future.error() == 0) {
              printf("User email address updated.");
            }
          },
          nullptr);
}

ส่งอีเมลยืนยันไปยังผู้ใช้

คุณสามารถส่งอีเมลยืนยันที่อยู่ไปยังผู้ใช้ด้วยเมธอด SendEmailVerification เช่น

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  user.SendEmailVerification().OnCompletion(
      [](const firebase::Future<void>& completed_future, void* user_data) {
        // We are probably in a different thread right now.
        if (completed_future.error() == 0) {
          printf("Email sent.");
        }
      },
      nullptr);
}

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

ตั้งค่ารหัสผ่านของผู้ใช้

คุณสามารถตั้งค่ารหัสผ่านของผู้ใช้ด้วยเมธอด UpdatePassword เช่น

firebase::auth::User user = auth->current_user();
std::string newPassword = "SOME-SECURE-PASSWORD";

if (user.is_valid()) {
  user.UpdatePassword(newPassword.c_str())
      .OnCompletion(
          [](const firebase::Future<void>& completed_future,
             void* user_data) {
            // We are probably in a different thread right now.
            if (completed_future.error() == 0) {
              printf("password updated.");
            }
          },
          nullptr);
}

ส่งอีเมลรีเซ็ตรหัสผ่าน

คุณสามารถส่งอีเมลรีเซ็ตรหัสผ่านไปยังผู้ใช้ด้วยเมธอด SendPasswordResetEmail เช่น

std::string emailAddress = "user@example.com";

auth->SendPasswordResetEmail(emailAddress.c_str())
    .OnCompletion(
        [](const firebase::Future<void>& completed_future,
           void* user_data) {
          // We are probably in a different thread right now.
          if (completed_future.error() == 0) {
            // Email sent.
          } else {
            // An error happened.
            printf("Error %d: %s", completed_future.error(),
                   completed_future.error_message());
          }
        },
        nullptr);

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

นอกจากนี้ คุณยังส่งอีเมลรีเซ็ตรหัสผ่านจากคอนโซล Firebase ได้ด้วย

ลบผู้ใช้

คุณสามารถลบบัญชีผู้ใช้ด้วยเมธอด Delete เช่น

firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
  user.Delete().OnCompletion(
      [](const firebase::Future<void>& completed_future, void* user_data) {
        if (completed_future.error() == 0) {
          // User deleted.
        } else {
          // An error happened.
          printf("Error %d: %s", completed_future.error(),
                 completed_future.error_message());
        }
      },
      nullptr);
}

นอกจากนี้ คุณยังลบผู้ใช้ในคอนโซลFirebase ได้ในแท็บ ความปลอดภัย > การตรวจสอบสิทธิ์ > ผู้ใช้

ตรวจสอบสิทธิ์ผู้ใช้ซ้ำ

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

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

firebase::auth::User user = auth->current_user();

// Get auth credentials from the user for re-authentication. The example
// below shows email and password credentials but there are multiple
// possible providers, such as GoogleAuthProvider or FacebookAuthProvider.
firebase::auth::Credential credential =
    firebase::auth::EmailAuthProvider::GetCredential("user@example.com",
                                                     "password1234");

if (user.is_valid()) {
  user.Reauthenticate(credential)
      .OnCompletion(
          [](const firebase::Future<void>& completed_future,
             void* user_data) {
            if (completed_future.error() == 0) {
              printf("User re-authenticated.");
            }
          },
          nullptr);
}

นำเข้าบัญชีผู้ใช้

คุณสามารถนำเข้าบัญชีผู้ใช้จากไฟล์ไปยังโปรเจ็กต์ Firebase ได้โดยใช้ คำสั่ง auth:import ของ Firebase CLI เช่น

firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14