फायरबेस में उपयोगकर्ताओं को प्रबंधित करें

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

आप CreateUserWithEmailAndPassword विधि को कॉल करके या Google साइन-इन या फेसबुक लॉगिन जैसे फ़ेडरेटेड पहचान प्रदाता का उपयोग करके पहली बार किसी उपयोगकर्ता में साइन इन करके अपने फायरबेस प्रोजेक्ट में एक नया उपयोगकर्ता बनाते हैं।

आप उपयोगकर्ता पृष्ठ पर फायरबेस कंसोल के प्रमाणीकरण अनुभाग से नए पासवर्ड-प्रमाणीकृत उपयोगकर्ता भी बना सकते हैं।

वर्तमान में साइन-इन किया हुआ उपयोगकर्ता प्राप्त करें

वर्तमान उपयोगकर्ता को प्राप्त करने का अनुशंसित तरीका ऑथ ऑब्जेक्ट पर श्रोता सेट करना है:

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

श्रोता का उपयोग करके, आप यह सुनिश्चित करते हैं कि जब आपको वर्तमान उपयोगकर्ता मिलता है तो ऑथ ऑब्जेक्ट किसी मध्यवर्ती स्थिति में नहीं है - जैसे आरंभीकरण।

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

उपयोगकर्ता का क्रेडेंशियल बनाए रखें

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

उपयोगकर्ता की प्रोफ़ाइल प्राप्त करें

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

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

आप किसी उपयोगकर्ता की मूल प्रोफ़ाइल जानकारी-उपयोगकर्ता का प्रदर्शन नाम और प्रोफ़ाइल फ़ोटो URL- को 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.");
  });
}

आप ईमेल टेम्प्लेट पेज पर फायरबेस कंसोल के प्रमाणीकरण अनुभाग में उपयोग किए जाने वाले ईमेल टेम्प्लेट को कस्टमाइज़ कर सकते हैं। फायरबेस सहायता केंद्र में ईमेल टेम्पलेट देखें।

उपयोगकर्ता का पासवर्ड सेट करें

आप 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.");
  });
}

आप ईमेल टेम्प्लेट पेज पर फायरबेस कंसोल के प्रमाणीकरण अनुभाग में उपयोग किए जाने वाले ईमेल टेम्प्लेट को कस्टमाइज़ कर सकते हैं। फायरबेस सहायता केंद्र में ईमेल टेम्पलेट देखें।

आप फायरबेस कंसोल से पासवर्ड रीसेट ईमेल भी भेज सकते हैं।

किसी उपयोगकर्ता को हटाएँ

आप 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.");
  });
}

आप उपयोगकर्ता पृष्ठ पर फ़ायरबेस कंसोल के प्रमाणीकरण अनुभाग से भी उपयोगकर्ताओं को हटा सकते हैं।

किसी उपयोगकर्ता को पुनः प्रमाणित करें

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

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