उपयोगकर्ता बनाना
नया उपयोगकर्ता बनाने के लिए, आपके पास ये विकल्प हैं:
अपने ऐप्लिकेशन से:
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(), 'गलत' दिखाएगा.
उपयोगकर्ता के क्रेडेंशियल को सेव रखना
उपयोगकर्ता के साइन इन करने के बाद, उसके क्रेडेंशियल लोकल कीस्टोर में सेव हो जाएंगे. उपयोगकर्ता को साइन आउट करके, उसके क्रेडेंशियल का लोकल कैश मिटाया जा सकता है. कीस्टोर, प्लैटफ़ॉर्म के हिसाब से होता है:
- Apple के प्लैटफ़ॉर्म: Keychain Services.
- Android: Android Keystore.
- Windows: Credential Management API.
- Keychain Services.
- Linux: libsecret, जिसे उपयोगकर्ता को इंस्टॉल करना होगा.
किसी उपयोगकर्ता की प्रोफ़ाइल देखना
किसी उपयोगकर्ता की प्रोफ़ाइल की जानकारी पाने के लिए, 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 कंसोल के Security > Authentication > Templates टैब में जाकर, यह तय किया जा सकता है कि किस ईमेल टेंप्लेट का इस्तेमाल किया जाए. 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 कंसोल के Security > Authentication > Templates टैब में जाकर, यह तय किया जा सकता है कि किस ईमेल टेंप्लेट का इस्तेमाल किया जाए. 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 कंसोल में जाकर भी उपयोगकर्ताओं को मिटाया जा सकता है. इसके लिए, सुरक्षा > पुष्टि > उपयोगकर्ता टैब पर जाएं.
उपयोगकर्ता की फिर से पुष्टि करना
सुरक्षा से जुड़ी कुछ संवेदनशील कार्रवाइयों के लिए, उपयोगकर्ता को हाल ही में साइन इन करना ज़रूरी है. जैसे, खाता मिटाना, मुख्य ईमेल पता सेट करना, और पासवर्ड बदलना. अगर इनमें से कोई कार्रवाई की जाती है और उपयोगकर्ता ने बहुत पहले साइन इन किया था, तो कार्रवाई पूरी नहीं होगी.
ऐसा होने पर, उपयोगकर्ता से साइन इन करने के नए क्रेडेंशियल लेकर, उसकी फिर से पुष्टि करें. साथ ही, क्रेडेंशियल को 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