Android पर Firebase से पुष्टि करने की सुविधा का इस्तेमाल शुरू करना

अपने ऐप्लिकेशन को Firebase से कनेक्ट करना

अगर आपने पहले से ऐसा नहीं किया है, तो अपने Android प्रोजेक्ट में Firebase जोड़ें.

अपने ऐप्लिकेशन में Firebase Authentication जोड़ना

  1. अपनी मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर <project>/<app-module>/build.gradle.kts या <project>/<app-module>/build.gradle) में, Android के लिए Firebase Authentication लाइब्रेरी की डिपेंडेंसी जोड़ें. हमारा सुझाव है कि लाइब्रेरी के वर्शन को कंट्रोल करने के लिए, Firebase Android BoM का इस्तेमाल करें.

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:34.0.0"))
    
        // Add the dependency for the Firebase Authentication library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-auth")
    }

    Firebase Android BoM का इस्तेमाल करने पर, आपका ऐप्लिकेशन हमेशा Firebase Android लाइब्रेरी के साथ काम करने वाले वर्शन का इस्तेमाल करेगा.

    (वैकल्पिक)  BoM का इस्तेमाल किए बिना, Firebase लाइब्रेरी की डिपेंडेंसी जोड़ें

    अगर आपको Firebase BoM का इस्तेमाल नहीं करना है, तो आपको डिपेंडेंसी लाइन में Firebase की हर लाइब्रेरी के वर्शन की जानकारी देनी होगी.

    ध्यान दें कि अगर आपके ऐप्लिकेशन में Firebase की एक से ज़्यादा लाइब्रेरी का इस्तेमाल किया जाता है, तो हमारा सुझाव है कि लाइब्रेरी के वर्शन मैनेज करने के लिए BoM का इस्तेमाल करें. इससे यह पक्का किया जा सकता है कि सभी वर्शन एक-दूसरे के साथ काम करते हों.

    dependencies {
        // Add the dependency for the Firebase Authentication library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-auth:24.0.0")
    }

  2. पुष्टि करने की सेवा देने वाली कंपनी का इस्तेमाल करने के लिए, आपको इसे Firebase कंसोल में चालू करना होगा. Firebase Authentication सेक्शन में मौजूद, साइन-इन करने का तरीका पेज पर जाएं. यहां से, ईमेल/पासवर्ड से साइन-इन करने की सुविधा और अपने ऐप्लिकेशन के लिए, पहचान की पुष्टि करने वाले अन्य प्रोवाइडर को चालू करें.

(ज़रूरी नहीं) Firebase Local Emulator Suite के साथ प्रोटोटाइप बनाना और उसकी जांच करना

इससे पहले कि हम यह बताएं कि आपका ऐप्लिकेशन उपयोगकर्ताओं की पुष्टि कैसे करता है, आइए हम आपको ऐसे टूल के बारे में बताते हैं जिनका इस्तेमाल करके, Authentication फ़ंक्शन का प्रोटोटाइप बनाया जा सकता है और उसे टेस्ट किया जा सकता है: Firebase Local Emulator Suite. अगर आपको पुष्टि करने के तरीकों और सेवा देने वाली कंपनियों में से किसी एक को चुनना है, Authentication और Firebase Security Rules का इस्तेमाल करके सार्वजनिक और निजी डेटा के साथ अलग-अलग डेटा मॉडल आज़माने हैं या साइन-इन यूज़र इंटरफ़ेस (यूआई) डिज़ाइन का प्रोटोटाइप बनाना है, तो लाइव सेवाओं को डिप्लॉय किए बिना स्थानीय तौर पर काम करना एक अच्छा विकल्प हो सकता है.

Authentication एम्युलेटर, Local Emulator Suite का हिस्सा होता है. इसकी मदद से, आपका ऐप्लिकेशन, एम्युलेट किए गए डेटाबेस के कॉन्टेंट और कॉन्फ़िगरेशन के साथ-साथ, एम्युलेट किए गए प्रोजेक्ट के संसाधनों (फ़ंक्शन, अन्य डेटाबेस, और सुरक्षा के नियम) के साथ इंटरैक्ट कर सकता है. हालांकि, ऐसा करना ज़रूरी नहीं है.

Authentication एम्युलेटर का इस्तेमाल करने के लिए, बस कुछ ही चरणों को पूरा करना होता है:

  1. एम्युलेटर से कनेक्ट करने के लिए, अपने ऐप्लिकेशन की टेस्ट कॉन्फ़िगरेशन में कोड की एक लाइन जोड़ें.
  2. अपनी लोकल प्रोजेक्ट डायरेक्ट्री के रूट से, firebase emulators:start चलाएं.
  3. इंटरैक्टिव प्रोटोटाइपिंग के लिए Local Emulator Suite यूज़र इंटरफ़ेस (यूआई) का इस्तेमाल करें या नॉन-इंटरैक्टिव टेस्टिंग के लिए Authentication एम्युलेटर REST API का इस्तेमाल करें.

ज़्यादा जानकारी के लिए, अपने ऐप्लिकेशन को Authentication एम्युलेटर से कनेक्ट करना लेख पढ़ें. ज़्यादा जानकारी के लिए, Local Emulator Suite परिचय देखें.

अब हम उपयोगकर्ताओं की पुष्टि करने के तरीके के बारे में बात करेंगे.

पुष्टि की मौजूदा स्थिति देखना

  1. FirebaseAuth का कोई इंस्टेंस घोषित करें.

    Kotlin

    private lateinit var auth: FirebaseAuth

    Java

    private FirebaseAuth mAuth;
  2. onCreate() तरीके में, FirebaseAuth इंस्टेंस को शुरू करें.

    Kotlin

    // Initialize Firebase Auth
    auth = Firebase.auth

    Java

    // Initialize Firebase Auth
    mAuth = FirebaseAuth.getInstance();
  3. अपनी गतिविधि शुरू करते समय, देखें कि उपयोगकर्ता ने फ़िलहाल साइन इन किया है या नहीं.

    Kotlin

    public override fun onStart() {
        super.onStart()
        // Check if user is signed in (non-null) and update UI accordingly.
        val currentUser = auth.currentUser
        if (currentUser != null) {
            reload()
        }
    }

    Java

    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();
        if(currentUser != null){
            reload();
        }
    }

नए उपयोगकर्ताओं को साइन अप करने के लिए न्योता देना

एक नई createAccount विधि बनाएं, जो ईमेल पता और पासवर्ड लेती है. इसके बाद, उनकी पुष्टि करती है और फिर createUserWithEmailAndPassword विधि का इस्तेमाल करके नया उपयोगकर्ता बनाती है.

Kotlin

auth.createUserWithEmailAndPassword(email, password)
    .addOnCompleteListener(this) { task ->
        if (task.isSuccessful) {
            // Sign in success, update UI with the signed-in user's information
            Log.d(TAG, "createUserWithEmail:success")
            val user = auth.currentUser
            updateUI(user)
        } else {
            // If sign in fails, display a message to the user.
            Log.w(TAG, "createUserWithEmail:failure", task.exception)
            Toast.makeText(
                baseContext,
                "Authentication failed.",
                Toast.LENGTH_SHORT,
            ).show()
            updateUI(null)
        }
    }

Java

mAuth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.d(TAG, "createUserWithEmail:success");
                    FirebaseUser user = mAuth.getCurrentUser();
                    updateUI(user);
                } else {
                    // If sign in fails, display a message to the user.
                    Log.w(TAG, "createUserWithEmail:failure", task.getException());
                    Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                            Toast.LENGTH_SHORT).show();
                    updateUI(null);
                }
            }
        });

नए उपयोगकर्ताओं को उनके ईमेल और पासवर्ड के साथ रजिस्टर करने के लिए एक फ़ॉर्म जोड़ें. साथ ही, जब यह फ़ॉर्म सबमिट किया जाए, तब इस नए तरीके को कॉल करें. इसका उदाहरण, हमारे क्विकस्टार्ट सैंपल में देखा जा सकता है.

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

एक नई signIn विधि बनाएं, जो ईमेल पता और पासवर्ड लेती है. इसके बाद, उनकी पुष्टि करती है और फिर signInWithEmailAndPassword विधि का इस्तेमाल करके उपयोगकर्ता को साइन इन करती है.

Kotlin

auth.signInWithEmailAndPassword(email, password)
    .addOnCompleteListener(this) { task ->
        if (task.isSuccessful) {
            // Sign in success, update UI with the signed-in user's information
            Log.d(TAG, "signInWithEmail:success")
            val user = auth.currentUser
            updateUI(user)
        } else {
            // If sign in fails, display a message to the user.
            Log.w(TAG, "signInWithEmail:failure", task.exception)
            Toast.makeText(
                baseContext,
                "Authentication failed.",
                Toast.LENGTH_SHORT,
            ).show()
            updateUI(null)
        }
    }

Java

mAuth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.d(TAG, "signInWithEmail:success");
                    FirebaseUser user = mAuth.getCurrentUser();
                    updateUI(user);
                } else {
                    // If sign in fails, display a message to the user.
                    Log.w(TAG, "signInWithEmail:failure", task.getException());
                    Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                            Toast.LENGTH_SHORT).show();
                    updateUI(null);
                }
            }
        });

उपयोगकर्ताओं को उनके ईमेल और पासवर्ड से साइन इन करने के लिए, एक फ़ॉर्म जोड़ें. साथ ही, फ़ॉर्म सबमिट होने पर इस नए तरीके को कॉल करें. इसका उदाहरण, हमारे क्विकस्टार्ट सैंपल में देखा जा सकता है.

उपयोगकर्ता की जानकारी ऐक्सेस करना

अगर कोई उपयोगकर्ता सफलतापूर्वक साइन इन कर लेता है, तो getCurrentUser तरीके का इस्तेमाल करके, उसके खाते का डेटा किसी भी समय पाया जा सकता है.

Kotlin

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

अगले चरण

पहचान और पुष्टि करने की अन्य सेवाएं जोड़ने के बारे में गाइड देखें: