Firebase में उपयोगकर्ताओं को मैनेज करें

उपयोगकर्ता बनाना

नया उपयोगकर्ता बनाने के लिए, आपके पास ये विकल्प हैं:

  • अपने ऐप्लिकेशन से: CreateUserWithEmailAndPassword तरीके को कॉल करके या फ़ेडरेटेड आइडेंटिटी प्रोवाइडर, जैसे कि Google साइन-इन या Facebook से लॉगिन करें का इस्तेमाल करके, पहली बार किसी उपयोगकर्ता को साइन इन करके, अपने Firebase प्रोजेक्ट में नया उपयोगकर्ता बनाएं.

  • Firebase कंसोल में: सुरक्षा > पुष्टि > उपयोगकर्ता टैब में जाकर, पासवर्ड से पुष्टि करने वाला नया उपयोगकर्ता बनाएं.

मौजूदा समय में साइन इन किए हुए उपयोगकर्ता की जानकारी पाना

मौजूदा उपयोगकर्ता की जानकारी पाने का सबसे सही तरीका यह है कि 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);

लिसनर का इस्तेमाल करके, यह पक्का किया जाता है कि मौजूदा उपयोगकर्ता को पाने के दौरान, Auth ऑब्जेक्ट किसी इंटरमीडिएट स्टेट में न हो. जैसे, शुरू होने की स्थिति में.

current_user को कॉल करके, मौजूदा समय में साइन-इन किए हुए उपयोगकर्ता की जानकारी भी पाई जा सकती है. अगर किसी उपयोगकर्ता ने साइन इन नहीं किया है, तो उपयोगकर्ता के is_valid तरीके से'गलत' वैल्यू मिलेगी.

यह कुकी, उपयोगकर्ता के क्रेडेंशियल को सेव करती है

उपयोगकर्ता के साइन इन करने के बाद, उसके क्रेडेंशियल लोकल कीस्टोर में सेव किए जाएंगे. उपयोगकर्ता को साइन आउट करके, उपयोगकर्ता के क्रेडेंशियल की लोकल कैश मेमोरी को मिटाया जा सकता है. कीस्टोर, प्लैटफ़ॉर्म के हिसाब से होता है:

किसी उपयोगकर्ता की प्रोफ़ाइल पाना

किसी उपयोगकर्ता की प्रोफ़ाइल की जानकारी पाने के लिए, 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();
  }
}

किसी उपयोगकर्ता की प्रोफ़ाइल अपडेट करना

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 console में जाकर भी उपयोगकर्ताओं को मिटाया जा सकता है. इसके लिए, सुरक्षा > पुष्टि करना > उपयोगकर्ता टैब पर जाएं.

किसी उपयोगकर्ता की फिर से पुष्टि करना

सुरक्षा से जुड़ी कुछ संवेदनशील कार्रवाइयों के लिए, उपयोगकर्ता को हाल ही में साइन इन करना ज़रूरी है. जैसे, खाता मिटाना, मुख्य ईमेल पता सेट करना, और पासवर्ड बदलना. इनमें से कोई कार्रवाई करने पर, अगर उपयोगकर्ता ने बहुत पहले साइन इन किया था, तो कार्रवाई पूरी नहीं होगी.

ऐसा होने पर, उपयोगकर्ता से साइन इन करने के नए क्रेडेंशियल लेकर उसकी फिर से पुष्टि करें. इसके बाद, उन क्रेडेंशियल को 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 प्रोजेक्ट में इंपोर्ट करने का विकल्प होता है. इसके लिए, Firebase CLI के auth:import कमांड का इस्तेमाल करें. उदाहरण के लिए:

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