एक उपयोगकर्ता बनाएँ
आप createUserWithEmailAndPassword
विधि को कॉल करके या पहली बार फ़ेडरेटेड पहचान प्रदाता, जैसे कि Google साइन-इन या Facebook लॉगिन का उपयोग करके किसी उपयोगकर्ता में साइन इन करके अपने Firebase प्रोजेक्ट में एक नया उपयोगकर्ता बनाते हैं।
आप उपयोगकर्ता पृष्ठ पर फायरबेस कंसोल के प्रमाणीकरण अनुभाग से नए पासवर्ड-प्रमाणित उपयोगकर्ता भी बना सकते हैं।
वर्तमान में साइन-इन उपयोगकर्ता प्राप्त करें
वर्तमान उपयोगकर्ता प्राप्त करने का अनुशंसित तरीका getCurrentUser
विधि को कॉल करना है। यदि कोई उपयोगकर्ता साइन इन नहीं है, getCurrentUser
शून्य देता है:
Kotlin+KTX
val user = Firebase.auth.currentUser if (user != null) { // User is signed in } else { // No user is signed in }
Java
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user != null) { // User is signed in } else { // No user is signed in }
ऐसे कुछ मामले हैं जहां getCurrentUser
एक गैर-शून्य FirebaseUser
लौटाएगा लेकिन अंतर्निहित टोकन मान्य नहीं है। यह हो सकता है, उदाहरण के लिए, यदि उपयोगकर्ता को किसी अन्य डिवाइस पर हटा दिया गया हो और स्थानीय टोकन ताज़ा नहीं किया गया हो। इस स्थिति में, आपको एक वैध उपयोगकर्ता getCurrentUser
मिल सकता है, लेकिन बाद में प्रमाणित संसाधनों के लिए कॉल विफल हो जाएगी।
getCurrentUser
भी null
हो सकता है क्योंकि ऑथ ऑब्जेक्ट प्रारंभ करना समाप्त नहीं हुआ है।
यदि आप एक AuthStateListener संलग्न करते हैं तो अंतर्निहित टोकन स्थिति में हर बार आपको कॉलबैक मिलेगा। ऊपर बताए गए मामलों की तरह बढ़त के मामलों पर प्रतिक्रिया करने के लिए यह उपयोगी हो सकता है।
उपयोगकर्ता का प्रोफ़ाइल प्राप्त करें
उपयोगकर्ता की प्रोफ़ाइल जानकारी प्राप्त करने के लिए, FirebaseUser
के उदाहरण के एक्सेसर विधियों का उपयोग करें। उदाहरण के लिए:
Kotlin+KTX
val user = Firebase.auth.currentUser user?.let { // Name, email address, and profile photo Url val name = it.displayName val email = it.email val photoUrl = it.photoUrl // Check if user's email is verified val emailVerified = it.isEmailVerified // 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 // FirebaseUser.getIdToken() instead. val uid = it.uid }
Java
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user != null) { // Name, email address, and profile photo Url String name = user.getDisplayName(); String email = user.getEmail(); Uri photoUrl = user.getPhotoUrl(); // Check if user's email is verified boolean emailVerified = user.isEmailVerified(); // 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 // FirebaseUser.getIdToken() instead. String uid = user.getUid(); }
उपयोगकर्ता की प्रदाता-विशिष्ट प्रोफ़ाइल जानकारी प्राप्त करें
उपयोगकर्ता से जुड़े साइन-इन प्रदाताओं से प्राप्त प्रोफ़ाइल जानकारी प्राप्त करने के लिए, getProviderData
पद्धति का उपयोग करें। उदाहरण के लिए:
Kotlin+KTX
val user = Firebase.auth.currentUser user?.let { for (profile in it.providerData) { // Id of the provider (ex: google.com) val providerId = profile.providerId // UID specific to the provider val uid = profile.uid // Name, email address, and profile photo Url val name = profile.displayName val email = profile.email val photoUrl = profile.photoUrl } }
Java
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user != null) { for (UserInfo profile : user.getProviderData()) { // Id of the provider (ex: google.com) String providerId = profile.getProviderId(); // UID specific to the provider String uid = profile.getUid(); // Name, email address, and profile photo Url String name = profile.getDisplayName(); String email = profile.getEmail(); Uri photoUrl = profile.getPhotoUrl(); } }
उपयोगकर्ता की प्रोफ़ाइल अपडेट करें
आप उपयोगकर्ता की बुनियादी प्रोफ़ाइल जानकारी—उपयोगकर्ता का प्रदर्शन नाम और प्रोफ़ाइल फ़ोटो URL— updateProfile
पद्धति से अपडेट कर सकते हैं। उदाहरण के लिए:
Kotlin+KTX
val user = Firebase.auth.currentUser val profileUpdates = userProfileChangeRequest { displayName = "Jane Q. User" photoUri = Uri.parse("https://example.com/jane-q-user/profile.jpg") } user!!.updateProfile(profileUpdates) .addOnCompleteListener { task -> if (task.isSuccessful) { Log.d(TAG, "User profile updated.") } }
Java
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder() .setDisplayName("Jane Q. User") .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg")) .build(); user.updateProfile(profileUpdates) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG, "User profile updated."); } } });
उपयोगकर्ता का ईमेल पता सेट करें
आप updateEmail
विधि से उपयोगकर्ता का ईमेल पता सेट कर सकते हैं। उदाहरण के लिए:
Kotlin+KTX
val user = Firebase.auth.currentUser user!!.updateEmail("user@example.com") .addOnCompleteListener { task -> if (task.isSuccessful) { Log.d(TAG, "User email address updated.") } }
Java
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); user.updateEmail("user@example.com") .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG, "User email address updated."); } } });
एक उपयोगकर्ता को एक सत्यापन ईमेल भेजें
आप sendEmailVerification
विधि के साथ किसी उपयोगकर्ता को पता सत्यापन ईमेल भेज सकते हैं। उदाहरण के लिए:
Kotlin+KTX
val user = Firebase.auth.currentUser user!!.sendEmailVerification() .addOnCompleteListener { task -> if (task.isSuccessful) { Log.d(TAG, "Email sent.") } }
Java
FirebaseAuth auth = FirebaseAuth.getInstance(); FirebaseUser user = auth.getCurrentUser(); user.sendEmailVerification() .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG, "Email sent."); } } });
आप ईमेल टेम्पलेट पृष्ठ पर फायरबेस कंसोल के प्रमाणीकरण अनुभाग में उपयोग किए जाने वाले ईमेल टेम्पलेट को कस्टमाइज़ कर सकते हैं। Firebase सहायता केंद्र में ईमेल टेम्प्लेट देखें।
सत्यापन ईमेल भेजते समय ऐप पर वापस रीडायरेक्ट करने के लिए जारी URL के माध्यम से राज्य पास करना भी संभव है।
इसके अतिरिक्त आप ईमेल भेजने से पहले प्रमाणीकरण उदाहरण पर भाषा कोड अपडेट करके सत्यापन ईमेल को स्थानीयकृत कर सकते हैं। उदाहरण के लिए:
Kotlin+KTX
auth.setLanguageCode("fr") // To apply the default app language instead of explicitly setting it. // auth.useAppLanguage()
Java
auth.setLanguageCode("fr"); // To apply the default app language instead of explicitly setting it. // auth.useAppLanguage();
उपयोगकर्ता का पासवर्ड सेट करें
आप updatePassword
विधि से उपयोगकर्ता का पासवर्ड सेट कर सकते हैं। उदाहरण के लिए:
Kotlin+KTX
val user = Firebase.auth.currentUser val newPassword = "SOME-SECURE-PASSWORD" user!!.updatePassword(newPassword) .addOnCompleteListener { task -> if (task.isSuccessful) { Log.d(TAG, "User password updated.") } }
Java
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); String newPassword = "SOME-SECURE-PASSWORD"; user.updatePassword(newPassword) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG, "User password updated."); } } });
एक पासवर्ड रीसेट ईमेल भेजें
आप किसी उपयोगकर्ता को sendPasswordResetEmail
पद्धति से पासवर्ड रीसेट ईमेल भेज सकते हैं। उदाहरण के लिए:
Kotlin+KTX
val emailAddress = "user@example.com" Firebase.auth.sendPasswordResetEmail(emailAddress) .addOnCompleteListener { task -> if (task.isSuccessful) { Log.d(TAG, "Email sent.") } }
Java
FirebaseAuth auth = FirebaseAuth.getInstance(); String emailAddress = "user@example.com"; auth.sendPasswordResetEmail(emailAddress) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG, "Email sent."); } } });
आप ईमेल टेम्पलेट पृष्ठ पर फायरबेस कंसोल के प्रमाणीकरण अनुभाग में उपयोग किए जाने वाले ईमेल टेम्पलेट को कस्टमाइज़ कर सकते हैं। Firebase सहायता केंद्र में ईमेल टेम्प्लेट देखें।
पासवर्ड रीसेट ईमेल भेजते समय ऐप पर वापस रीडायरेक्ट करने के लिए जारी URL के माध्यम से राज्य पास करना भी संभव है।
इसके अतिरिक्त आप ईमेल भेजने से पहले प्रमाणीकरण उदाहरण पर भाषा कोड अपडेट करके पासवर्ड रीसेट ईमेल को स्थानीयकृत कर सकते हैं। उदाहरण के लिए:
Kotlin+KTX
auth.setLanguageCode("fr") // To apply the default app language instead of explicitly setting it. // auth.useAppLanguage()
Java
auth.setLanguageCode("fr"); // To apply the default app language instead of explicitly setting it. // auth.useAppLanguage();
आप फायरबेस कंसोल से पासवर्ड रीसेट ईमेल भी भेज सकते हैं।
एक उपयोगकर्ता हटाएं
आप delete
मेथड से यूजर अकाउंट को डिलीट कर सकते हैं। उदाहरण के लिए:
Kotlin+KTX
val user = Firebase.auth.currentUser!! user.delete() .addOnCompleteListener { task -> if (task.isSuccessful) { Log.d(TAG, "User account deleted.") } }
Java
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); user.delete() .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG, "User account deleted."); } } });
आप उपयोगकर्ता पृष्ठ पर Firebase कंसोल के प्रमाणीकरण अनुभाग से भी उपयोगकर्ताओं को हटा सकते हैं।
एक उपयोगकर्ता को फिर से प्रमाणित करें
कुछ सुरक्षा-संवेदनशील कार्रवाइयाँ—जैसे खाता हटाना , प्राथमिक ईमेल पता सेट करना और पासवर्ड बदलना—आवश्यक होता है कि उपयोगकर्ता ने हाल ही में साइन इन किया हो। क्रिया विफल हो जाती है और FirebaseAuthRecentLoginRequiredException
फेंकता है। जब ऐसा होता है, तो उपयोगकर्ता से नए साइन-इन क्रेडेंशियल्स प्राप्त करके और फिर से प्रमाणित करने के लिए क्रेडेंशियल पास करके उपयोगकर्ता को फिर से reauthenticate
करें। उदाहरण के लिए:
Kotlin+KTX
val user = Firebase.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. val credential = EmailAuthProvider .getCredential("user@example.com", "password1234") // Prompt the user to re-provide their sign-in credentials user.reauthenticate(credential) .addOnCompleteListener { Log.d(TAG, "User re-authenticated.") }
Java
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); // 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. AuthCredential credential = EmailAuthProvider .getCredential("user@example.com", "password1234"); // Prompt the user to re-provide their sign-in credentials user.reauthenticate(credential) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { Log.d(TAG, "User re-authenticated."); } });
उपयोगकर्ता खाते आयात करें
आप फायरबेस सीएलआई के auth:import
कमांड का उपयोग करके उपयोगकर्ता खातों को अपने फायरबेस प्रोजेक्ट में फ़ाइल से आयात कर सकते हैं। उदाहरण के लिए:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14