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

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

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

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

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

वर्तमान उपयोगकर्ता प्राप्त करने का अनुशंसित तरीका 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();
    }
}

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

आप updateProfile पद्धति से उपयोगकर्ता की मूल प्रोफ़ाइल जानकारी—उपयोगकर्ता का प्रदर्शन नाम और प्रोफ़ाइल फ़ोटो URL—अपडेट कर सकते हैं। उदाहरण के लिए:

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

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

सत्यापन ईमेल भेजते समय ऐप पर वापस रीडायरेक्ट करने के लिए जारी यूआरएल के माध्यम से राज्य को पास करना भी संभव है।

इसके अतिरिक्त आप ईमेल भेजने से पहले प्रामाणिक उदाहरण पर भाषा कोड को अपडेट करके सत्यापन ईमेल को स्थानीयकृत कर सकते हैं। उदाहरण के लिए:

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

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

पासवर्ड रीसेट ईमेल भेजते समय ऐप पर वापस रीडायरेक्ट करने के लिए जारी यूआरएल के माध्यम से राज्य को पास करना भी संभव है।

इसके अतिरिक्त आप ईमेल भेजने से पहले प्रामाणिक उदाहरण पर भाषा कोड को अपडेट करके पासवर्ड रीसेट ईमेल को स्थानीयकृत कर सकते हैं। उदाहरण के लिए:

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

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

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

कुछ सुरक्षा-संवेदनशील कार्रवाइयां—जैसे खाता हटाना , प्राथमिक ईमेल पता सेट करना और पासवर्ड बदलना —के लिए आवश्यक है कि उपयोगकर्ता ने हाल ही में साइन इन किया हो। यदि आप इनमें से कोई एक क्रिया करते हैं, और उपयोगकर्ता ने बहुत पहले साइन इन किया है, कार्रवाई विफल हो जाती है और 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.");
            }
        });

उपयोगकर्ता खाते आयात करें

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

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