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

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

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

उपयोगकर्ता पेज पर, Firebase कंसोल के पुष्टि सेक्शन से भी, पासवर्ड से पुष्टि किए गए नए उपयोगकर्ता बनाए जा सकते हैं.

वर्तमान में प्रवेश किए हुए उपयोगकर्ता को पाएं

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

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

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

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

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

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

ऐसा होने पर, उपयोगकर्ता से नए साइन-इन क्रेडेंशियल लेकर और 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 प्रोजेक्ट में उपयोगकर्ता खातों को इंपोर्ट किया जा सकता है. उदाहरण के लिए:

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