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

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

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

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

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

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

मौजूदा उपयोगकर्ता की जानकारी पाने का सबसे सही तरीका यह है कि Auth ऑब्जेक्ट पर लिसनर सेट किया जाए:

Firebase.Auth.FirebaseAuth auth;
Firebase.Auth.FirebaseUser user;

// Handle initialization of the necessary firebase modules:
void InitializeFirebase() {
  Debug.Log("Setting up Firebase Auth");
  auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
  auth.StateChanged += AuthStateChanged;
  AuthStateChanged(this, null);
}

// Track state changes of the auth object.
void AuthStateChanged(object sender, System.EventArgs eventArgs) {
  if (auth.CurrentUser != user) {
    bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
    if (!signedIn && user != null) {
      Debug.Log("Signed out " + user.UserId);
    }
    user = auth.CurrentUser;
    if (signedIn) {
      Debug.Log("Signed in " + user.UserId);
    }
  }
}

// Handle removing subscription and reference to the Auth instance.
// Automatically called by a Monobehaviour after Destroy is called on it.
void OnDestroy() {
  auth.StateChanged -= AuthStateChanged;
  auth = null;
}

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

CurrentUser को कॉल करके, मौजूदा समय में साइन इन किए हुए उपयोगकर्ता की जानकारी भी पाई जा सकती है. अगर किसी उपयोगकर्ता ने साइन इन नहीं किया है, तो CurrentUser शून्य वैल्यू दिखाएगा. अगर कोई उपयोगकर्ता साइन आउट है, तो उपयोगकर्ता के IsValid() की वैल्यू false दिखेगी.

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

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

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

किसी उपयोगकर्ता की प्रोफ़ाइल की जानकारी पाने के लिए, Firebase.Auth.FirebaseUser के किसी इंस्टेंस के ऐक्सेसर तरीकों का इस्तेमाल करें. उदाहरण के लिए:

Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null) {
  string name = user.DisplayName;
  string email = user.Email;
  System.Uri photo_url = user.PhotoUrl;
  // 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 User.TokenAsync() instead.
  string uid = user.UserId;
}

किसी उपयोगकर्ता की, सेवा देने वाली कंपनी के हिसाब से प्रोफ़ाइल की जानकारी पाना

किसी उपयोगकर्ता से लिंक किए गए साइन-इन की सुविधा देने वाली कंपनियों से मिली प्रोफ़ाइल की जानकारी पाने के लिए, ProviderData तरीके का इस्तेमाल करें. उदाहरण के लिए:

Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null) {
  foreach (var profile in user.ProviderData) {
    // Id of the provider (ex: google.com)
    string providerId = profile.ProviderId;

    // UID specific to the provider
    string uid = profile.UserId;

    // Name, email address, and profile photo Url
    string name = profile.DisplayName;
    string email = profile.Email;
    System.Uri photoUrl = profile.PhotoUrl;
  }
}

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

UpdateUserProfile तरीके का इस्तेमाल करके, किसी उपयोगकर्ता की प्रोफ़ाइल की बुनियादी जानकारी अपडेट की जा सकती है. जैसे, उपयोगकर्ता का डिसप्ले नेम और प्रोफ़ाइल फ़ोटो का यूआरएल. उदाहरण के लिए:

Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null) {
  Firebase.Auth.UserProfile profile = new Firebase.Auth.UserProfile {
    DisplayName = "Jane Q. User",
    PhotoUrl = new System.Uri("https://example.com/jane-q-user/profile.jpg"),
  };
  user.UpdateUserProfileAsync(profile).ContinueWith(task => {
    if (task.IsCanceled) {
      Debug.LogError("UpdateUserProfileAsync was canceled.");
      return;
    }
    if (task.IsFaulted) {
      Debug.LogError("UpdateUserProfileAsync encountered an error: " + task.Exception);
      return;
    }

    Debug.Log("User profile updated successfully.");
  });
}

किसी उपयोगकर्ता का ईमेल पता सेट करना

UpdateEmail तरीके का इस्तेमाल करके, किसी उपयोगकर्ता का ईमेल पता सेट किया जा सकता है. उदाहरण के लिए:

Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null) {
  user.UpdateEmailAsync("user@example.com").ContinueWith(task => {
    if (task.IsCanceled) {
      Debug.LogError("UpdateEmailAsync was canceled.");
      return;
    }
    if (task.IsFaulted) {
      Debug.LogError("UpdateEmailAsync encountered an error: " + task.Exception);
      return;
    }

    Debug.Log("User email updated successfully.");
  });
}

किसी उपयोगकर्ता को पुष्टि करने के लिए ईमेल भेजना

SendEmailVerification तरीके का इस्तेमाल करके, किसी उपयोगकर्ता को पते की पुष्टि करने वाला ईमेल भेजा जा सकता है. उदाहरण के लिए:

Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null) {
  user.SendEmailVerificationAsync().ContinueWith(task => {
    if (task.IsCanceled) {
      Debug.LogError("SendEmailVerificationAsync was canceled.");
      return;
    }
    if (task.IsFaulted) {
      Debug.LogError("SendEmailVerificationAsync encountered an error: " + task.Exception);
      return;
    }

    Debug.Log("Email sent successfully.");
  });
}

Firebase कंसोल में, सुरक्षा > पुष्टि > टेंप्लेट टैब में जाकर, अपनी पसंद के मुताबिक ईमेल टेंप्लेट का इस्तेमाल किया जा सकता है. Firebase सहायता केंद्र में ईमेल टेंप्लेट देखें.

किसी उपयोगकर्ता का पासवर्ड सेट करना

UpdatePassword तरीके का इस्तेमाल करके, किसी उपयोगकर्ता का पासवर्ड सेट किया जा सकता है. उदाहरण के लिए:

Firebase.Auth.FirebaseUser user = auth.CurrentUser;
string newPassword = "SOME-SECURE-PASSWORD";
if (user != null) {
  user.UpdatePasswordAsync(newPassword).ContinueWith(task => {
    if (task.IsCanceled) {
      Debug.LogError("UpdatePasswordAsync was canceled.");
      return;
    }
    if (task.IsFaulted) {
      Debug.LogError("UpdatePasswordAsync encountered an error: " + task.Exception);
      return;
    }

    Debug.Log("Password updated successfully.");
  });
}

पासवर्ड रीसेट करने का ईमेल भेजना

SendPasswordResetEmail तरीके का इस्तेमाल करके, किसी उपयोगकर्ता को पासवर्ड रीसेट करने का ईमेल भेजा जा सकता है. उदाहरण के लिए:

string emailAddress = "user@example.com";
if (user != null) {
  auth.SendPasswordResetEmailAsync(emailAddress).ContinueWith(task => {
    if (task.IsCanceled) {
      Debug.LogError("SendPasswordResetEmailAsync was canceled.");
      return;
    }
    if (task.IsFaulted) {
      Debug.LogError("SendPasswordResetEmailAsync encountered an error: " + task.Exception);
      return;
    }

    Debug.Log("Password reset email sent successfully.");
  });
}

Firebase कंसोल में, सुरक्षा > पुष्टि > टेंप्लेट टैब में जाकर, अपनी पसंद के मुताबिक ईमेल टेंप्लेट का इस्तेमाल किया जा सकता है. Firebase सहायता केंद्र में ईमेल टेंप्लेट देखें.

Firebase कंसोल से भी, पासवर्ड रीसेट करने के ईमेल भेजे जा सकते हैं.

किसी उपयोगकर्ता को मिटाना

Delete तरीके का इस्तेमाल करके, किसी उपयोगकर्ता खाते को मिटाया जा सकता है. उदाहरण के लिए:

Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null) {
  user.DeleteAsync().ContinueWith(task => {
    if (task.IsCanceled) {
      Debug.LogError("DeleteAsync was canceled.");
      return;
    }
    if (task.IsFaulted) {
      Debug.LogError("DeleteAsync encountered an error: " + task.Exception);
      return;
    }

    Debug.Log("User deleted successfully.");
  });
}

सुरक्षा > पुष्टि > उपयोगकर्ता टैब में जाकर, Firebase console में भी उपयोगकर्ताओं को मिटाया जा सकता है.

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

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

ऐसा होने पर, उपयोगकर्ता से साइन इन करने के नए क्रेडेंशियल लेकर उसकी फिर से पुष्टि करें. इसके बाद, उन क्रेडेंशियल को Reauthenticate को भेजें. उदाहरण के लिए:

Firebase.Auth.FirebaseUser user = auth.CurrentUser;

// 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 != null) {
  user.ReauthenticateAsync(credential).ContinueWith(task => {
    if (task.IsCanceled) {
      Debug.LogError("ReauthenticateAsync was canceled.");
      return;
    }
    if (task.IsFaulted) {
      Debug.LogError("ReauthenticateAsync encountered an error: " + task.Exception);
      return;
    }

    Debug.Log("User reauthenticated successfully.");
  });
}

उपयोगकर्ता खाते इंपोर्ट करना

Firebase CLI की auth:import कमांड का इस्तेमाल करके, किसी फ़ाइल से उपयोगकर्ता खातों को अपने Firebase प्रोजेक्ट में इंपोर्ट किया जा सकता है. उदाहरण के लिए:

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