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

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

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

उपयोगकर्ता पेज पर, Firebase console के पुष्टि करने वाले सेक्शन में जाकर, पासवर्ड की मदद से पुष्टि करने वाले नए उपयोगकर्ता भी बनाए जा सकते हैं.

मौजूदा साइन इन उपयोगकर्ता की जानकारी पाना

हमारा सुझाव है कि मौजूदा उपयोगकर्ता की जानकारी पाने के लिए, getCurrentUser तरीके को कॉल करें. अगर किसी भी उपयोगकर्ता ने साइन इन नहीं किया है, तो getCurrentUser शून्य दिखाता है:

KotlinJava
val user = Firebase.auth.currentUser
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}
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 के किसी इंस्टेंस के ऐक्सेस करने वाले तरीकों का इस्तेमाल करें. उदाहरण के लिए:

KotlinJava
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
}
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 तरीके का इस्तेमाल करें. उदाहरण के लिए:

KotlinJava
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
    }
}
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 तरीके का इस्तेमाल करके, उपयोगकर्ता की प्रोफ़ाइल की बुनियादी जानकारी अपडेट की जा सकती है. जैसे, उपयोगकर्ता का डिसप्ले नेम और प्रोफ़ाइल फ़ोटो का यूआरएल. उदाहरण के लिए:

KotlinJava
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.")
        }
    }
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 तरीके से, उपयोगकर्ता का ईमेल पता सेट किया जा सकता है. उदाहरण के लिए:

KotlinJava
val user = Firebase.auth.currentUser

user!!.updateEmail("user@example.com")
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "User email address updated.")
        }
    }
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 तरीके का इस्तेमाल करके, किसी उपयोगकर्ता को पते की पुष्टि करने के लिए ईमेल भेजा जा सकता है. उदाहरण के लिए:

KotlinJava
val user = Firebase.auth.currentUser

user!!.sendEmailVerification()
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "Email sent.")
        }
    }
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 कंसोल के पुष्टि करने वाले सेक्शन में इस्तेमाल किए जाने वाले ईमेल टेंप्लेट को पसंद के मुताबिक बनाया जा सकता है. इसके लिए, ईमेल टेंप्लेट पेज पर जाएं. Firebase के सहायता केंद्र में, ईमेल टेंप्लेट देखें.

पुष्टि करने के लिए ईमेल भेजते समय, उपयोगकर्ता को ऐप्लिकेशन पर वापस रीडायरेक्ट करने के लिए, जारी रखें यूआरएल के ज़रिए स्टेटस पास किया जा सकता है.

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

KotlinJava
auth.setLanguageCode("fr")
// To apply the default app language instead of explicitly setting it.
// auth.useAppLanguage()
auth.setLanguageCode("fr");
// To apply the default app language instead of explicitly setting it.
// auth.useAppLanguage();

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

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

KotlinJava
val user = Firebase.auth.currentUser
val newPassword = "SOME-SECURE-PASSWORD"

user!!.updatePassword(newPassword)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "User password updated.")
        }
    }
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 तरीके का इस्तेमाल करके, किसी उपयोगकर्ता को पासवर्ड रीसेट करने का ईमेल भेजा जा सकता है. उदाहरण के लिए:

KotlinJava
val emailAddress = "user@example.com"

Firebase.auth.sendPasswordResetEmail(emailAddress)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "Email sent.")
        }
    }
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 कंसोल के पुष्टि करने वाले सेक्शन में इस्तेमाल किए जाने वाले ईमेल टेंप्लेट को पसंद के मुताबिक बनाया जा सकता है. इसके लिए, ईमेल टेंप्लेट पेज पर जाएं. Firebase के सहायता केंद्र में, ईमेल टेंप्लेट देखें.

पासवर्ड रीसेट करने का ईमेल भेजते समय, ऐप्लिकेशन पर वापस रीडायरेक्ट करने के लिए, जारी रखें यूआरएल के ज़रिए स्टेटस पास किया जा सकता है.

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

KotlinJava
auth.setLanguageCode("fr")
// To apply the default app language instead of explicitly setting it.
// auth.useAppLanguage()
auth.setLanguageCode("fr");
// To apply the default app language instead of explicitly setting it.
// auth.useAppLanguage();

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

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

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

KotlinJava
val user = Firebase.auth.currentUser!!

user.delete()
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "User account deleted.")
        }
    }
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 console के पुष्टि करने की प्रक्रिया वाले सेक्शन से भी उपयोगकर्ताओं को मिटाया जा सकता है.

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

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

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