अपने Android ऐप्लिकेशन में बहु-स्तरीय पुष्टि (MFA) जोड़ें

अगर आपने Firebase Authentication with Identity Platform में अपग्रेड किया है, तो अपने Android ऐप्लिकेशन में, मैसेज के ज़रिए दो चरणों में पुष्टि करने की सुविधा जोड़ी जा सकती है.

दो चरणों में पुष्टि की सुविधा से, आपके ऐप्लिकेशन की सुरक्षा बढ़ जाती है. अक्सर, हमलावर (हैकर) पासवर्ड और सोशल मीडिया खातों को हैक कर लेते हैं. हालांकि, मैसेज को इंटरसेप्ट करना ज़्यादा मुश्किल होता है.

शुरू करने से पहले

  1. कम से कम एक ऐसा प्रोवाइडर चालू करें जो दो चरणों में पुष्टि की सुविधा के साथ काम करता हो. हर प्रोवाइडर, दो चरणों में पुष्टि की सुविधा के साथ काम करता है. हालांकि , फ़ोन से पुष्टि करने, गुमनाम तरीके से पुष्टि करने, और Apple Game Center से पुष्टि करने की सुविधा के साथ काम नहीं करता.

  2. पक्का करें कि आपका ऐप्लिकेशन, उपयोगकर्ताओं के ईमेल पतों की पुष्टि कर रहा हो. दो चरणों में पुष्टि की सुविधा के लिए, ईमेल पते की पुष्टि करना ज़रूरी है. इससे, गलत इरादे से काम करने वाले लोग, ऐसे ईमेल पते से किसी सेवा के लिए रजिस्टर नहीं कर पाते जिसका मालिकाना हक उनके पास नहीं है. इसके बाद, वे पुष्टि करने का दूसरा चरण जोड़कर, असली मालिक को उसके खाते से बाहर नहीं कर पाते.

  3. Firebase कंसोल में, अपने ऐप्लिकेशन का SHA-1 हैश रजिस्टर करें. आपके बदलाव, Google Cloud Firebase में अपने-आप दिखेंगे.

    1. अपने ऐप्लिकेशन का SHA-1 हैश पाने के लिए, क्लाइंट की पुष्टि करने का तरीका अपनाएं.

    2. Firebase कंसोल खोलें.

    3. प्रोजेक्ट सेटिंग पर जाएं.

    4. आपके ऐप्लिकेशन में जाकर, Android के आइकॉन पर क्लिक करें.

    5. अपना SHA-1 हैश जोड़ने के लिए, दिए गए निर्देशों का पालन करें.

दो चरणों में पुष्टि की सुविधा चालू करना

  1. Firebase कंसोल में, पुष्टि > साइन-इन करने का तरीका पेज खोलें.

  2. बेहतर सेक्शन में जाकर, मैसेज के ज़रिए दो चरणों में पुष्टि की सुविधा चालू करें.

    आपको उन फ़ोन नंबरों को भी डालना होगा जिनसे अपने ऐप्लिकेशन की जांच की जाएगी. हालांकि, टेस्ट फ़ोन नंबर रजिस्टर करना ज़रूरी नहीं है. फिर भी, डेवलपमेंट के दौरान थ्रॉटलिंग से बचने के लिए, ऐसा करने का सुझाव दिया जाता है.

  3. अगर आपने अपने ऐप्लिकेशन के डोमेन को पहले से ही अनुमति नहीं दी है, तो Firebase कंसोल में पुष्टि > सेटिंग पेज पर जाकर, उसे अनुमति वाली सूची में जोड़ें.

  4. ज़रूरी नहीं: पुष्टि > सेटिंग पेज पर जाकर, उन इलाकों के लिए कोई नीति सेट करें जहां मैसेज के ज़रिए दो चरणों में पुष्टि की सुविधा चालू या बंद करनी है. मैसेज के ज़रिए दो चरणों में पुष्टि की सुविधा के लिए, इलाके की नीति सेट करने से, आपके ऐप्लिकेशन को मैसेज के गलत इस्तेमाल से बचाया जा सकता है.

रजिस्ट्रेशन का पैटर्न चुनना

आपके पास यह चुनने का विकल्प होता है कि आपके ऐप्लिकेशन के लिए, दो चरणों में पुष्टि की सुविधा ज़रूरी है या नहीं. साथ ही, यह भी तय किया जा सकता है कि उपयोगकर्ताओं को कब और कैसे रजिस्टर करना है. कुछ सामान्य पैटर्न यहां दिए गए हैं:

  • रजिस्ट्रेशन के दौरान, उपयोगकर्ता के दूसरे चरण की जानकारी रजिस्टर करें. अगर आपके ऐप्लिकेशन के लिए, सभी उपयोगकर्ताओं के लिए दो चरणों में पुष्टि की सुविधा ज़रूरी है, तो इस तरीके का इस्तेमाल करें.

  • रजिस्ट्रेशन के दौरान, दूसरे चरण की जानकारी रजिस्टर करने के लिए, स्किप करने का विकल्प दें. ऐसे ऐप्लिकेशन जो दो चरणों में पुष्टि की सुविधा को बढ़ावा देना चाहते हैं, लेकिन इसे ज़रूरी नहीं बनाना चाहते उनके लिए यह तरीका बेहतर हो सकता है.

  • साइन अप स्क्रीन के बजाय, उपयोगकर्ता के खाते या प्रोफ़ाइल मैनेजमेंट पेज से, दूसरा चरण जोड़ने की सुविधा दें. इससे रजिस्ट्रेशन की प्रोसेस के दौरान होने वाली मुश्किलों को कम किया जा सकता है. साथ ही, सुरक्षा को लेकर संवेदनशील उपयोगकर्ताओं के लिए, दो चरणों में पुष्टि की सुविधा उपलब्ध कराई जा सकती है.

  • जब उपयोगकर्ता को सुरक्षा से जुड़ी ज़्यादा ज़रूरी शर्तों वाली सुविधाओं को ऐक्सेस करना हो, तब उसे दूसरा चरण जोड़ने के लिए कहें.

दूसरा चरण रजिस्टर करना

किसी उपयोगकर्ता के लिए, पुष्टि करने का नया दूसरा चरण रजिस्टर करने के लिए:

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

  2. उपयोगकर्ता से उसका फ़ोन नंबर डालने के लिए कहें.

  3. उपयोगकर्ता के लिए, दो चरणों में पुष्टि की सुविधा वाला सेशन पाएं:

    Kotlin

    user.multiFactor.session.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val multiFactorSession: MultiFactorSession? = task.result
        }
    }
    

    Java

    user.getMultiFactor().getSession()
      .addOnCompleteListener(
          new OnCompleteListener<MultiFactorSession>() {
          @Override
          public void onComplete(@NonNull Task<MultiFactorSession> task) {
            if (task.isSuccessful()) {
              MultiFactorSession multiFactorSession = task.getResult();
            }
          }
          });
    
  4. पुष्टि की प्रोसेस में अलग-अलग इवेंट को मैनेज करने के लिए, OnVerificationStateChangedCallbacks ऑब्जेक्ट बनाएं:

    Kotlin

    val callbacks = object : OnVerificationStateChangedCallbacks() {
        override fun onVerificationCompleted(credential: PhoneAuthCredential) {
            // This callback will be invoked in two situations:
            // 1) Instant verification. In some cases, the phone number can be
            //    instantly verified without needing to send or enter a verification
            //    code. You can disable this feature by calling
            //    PhoneAuthOptions.builder#requireSmsValidation(true) when building
            //    the options to pass to PhoneAuthProvider#verifyPhoneNumber().
            // 2) Auto-retrieval. On some devices, Google Play services can
            //    automatically detect the incoming verification SMS and perform
            //    verification without user action.
            this@MainActivity.credential = credential
        }
    
        override fun onVerificationFailed(e: FirebaseException) {
            // This callback is invoked in response to invalid requests for
            // verification, like an incorrect phone number.
            if (e is FirebaseAuthInvalidCredentialsException) {
                // Invalid request
                // ...
            } else if (e is FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded
                // ...
            }
            // Show a message and update the UI
            // ...
        }
    
        override fun onCodeSent(
            verificationId: String, forceResendingToken: ForceResendingToken
        ) {
            // The SMS verification code has been sent to the provided phone number.
            // We now need to ask the user to enter the code and then construct a
            // credential by combining the code with a verification ID.
            // Save the verification ID and resending token for later use.
            this@MainActivity.verificationId = verificationId
            this@MainActivity.forceResendingToken = forceResendingToken
            // ...
        }
    }
    

    Java

    OnVerificationStateChangedCallbacks callbacks =
    new OnVerificationStateChangedCallbacks() {
      @Override
      public void onVerificationCompleted(PhoneAuthCredential credential) {
        // This callback will be invoked in two situations:
        // 1) Instant verification. In some cases, the phone number can be
        //    instantly verified without needing to send or enter a verification
        //    code. You can disable this feature by calling
        //    PhoneAuthOptions.builder#requireSmsValidation(true) when building
        //    the options to pass to PhoneAuthProvider#verifyPhoneNumber().
        // 2) Auto-retrieval. On some devices, Google Play services can
        //    automatically detect the incoming verification SMS and perform
        //    verification without user action.
        this.credential = credential;
      }
      @Override
      public void onVerificationFailed(FirebaseException e) {
        // This callback is invoked in response to invalid requests for
        // verification, like an incorrect phone number.
        if (e instanceof FirebaseAuthInvalidCredentialsException) {
        // Invalid request
        // ...
        } else if (e instanceof FirebaseTooManyRequestsException) {
        // The SMS quota for the project has been exceeded
        // ...
        }
        // Show a message and update the UI
        // ...
      }
      @Override
      public void onCodeSent(
        String verificationId, PhoneAuthProvider.ForceResendingToken token) {
        // The SMS verification code has been sent to the provided phone number.
        // We now need to ask the user to enter the code and then construct a
        // credential by combining the code with a verification ID.
        // Save the verification ID and resending token for later use.
        this.verificationId = verificationId;
        this.forceResendingToken = token;
        // ...
      }
    };
    
  5. PhoneInfoOptions ऑब्जेक्ट को, उपयोगकर्ता के फ़ोन नंबर, दो चरणों में पुष्टि की सुविधा वाले सेशन, और अपने कॉलबैक के साथ शुरू करें:

    Kotlin

    val phoneAuthOptions = PhoneAuthOptions.newBuilder()
        .setPhoneNumber(phoneNumber)
        .setTimeout(30L, TimeUnit.SECONDS)
        .setMultiFactorSession(MultiFactorSession)
        .setCallbacks(callbacks)
        .build()
    

    Java

    PhoneAuthOptions phoneAuthOptions =
      PhoneAuthOptions.newBuilder()
          .setPhoneNumber(phoneNumber)
          .setTimeout(30L, TimeUnit.SECONDS)
          .setMultiFactorSession(multiFactorSession)
          .setCallbacks(callbacks)
          .build();
    

    डिफ़ॉल्ट रूप से, तुरंत पुष्टि की सुविधा चालू होती है. इसे बंद करने के लिए, requireSmsValidation(true) में कॉल जोड़ें.

  6. उपयोगकर्ता के फ़ोन पर, पुष्टि करने का मैसेज भेजें:

    Kotlin

    PhoneAuthProvider.verifyPhoneNumber(phoneAuthOptions)
    

    Java

    PhoneAuthProvider.verifyPhoneNumber(phoneAuthOptions);
    

    हालांकि, यह ज़रूरी नहीं है. फिर भी, उपयोगकर्ताओं को पहले से यह बताना सबसे सही तरीका है कि उन्हें एक मैसेज मिलेगा और इस पर सामान्य शुल्क लागू होंगे.

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

    Kotlin

    // Ask user for the verification code.
    val credential = PhoneAuthProvider.getCredential(verificationId, verificationCode)
    

    Java

    // Ask user for the verification code.
    PhoneAuthCredential credential
      = PhoneAuthProvider.getCredential(verificationId, verificationCode);
    
  8. PhoneAuthCredential के साथ, MultiFactorAssertion ऑब्जेक्ट शुरू करें:

    Kotlin

    val multiFactorAssertion
      = PhoneMultiFactorGenerator.getAssertion(credential)
    

    Java

    MultiFactorAssertion multiFactorAssertion
      = PhoneMultiFactorGenerator.getAssertion(credential);
    
  9. रजिस्ट्रेशन पूरा करें. ज़रूरी नहीं है कि दूसरे चरण के लिए कोई डिसप्ले नेम तय किया जाए. यह उन उपयोगकर्ताओं के लिए काम का है जिनके पास पुष्टि करने के लिए एक से ज़्यादा दूसरे चरण हैं. ऐसा इसलिए, क्योंकि पुष्टि की प्रोसेस के दौरान फ़ोन नंबर मास्क किया जाता है. उदाहरण के लिए, +1******1234.

    Kotlin

    // Complete enrollment. This will update the underlying tokens
    // and trigger ID token change listener.
    FirebaseAuth.getInstance()
        .currentUser
        ?.multiFactor
        ?.enroll(multiFactorAssertion, "My personal phone number")
        ?.addOnCompleteListener {
            // ...
        }
    

    Java

    // Complete enrollment. This will update the underlying tokens
    // and trigger ID token change listener.
    FirebaseAuth.getInstance()
      .getCurrentUser()
      .getMultiFactor()
      .enroll(multiFactorAssertion, "My personal phone number")
      .addOnCompleteListener(
          new OnCompleteListener<Void>() {
          @Override
          public void onComplete(@NonNull Task<Void> task) {
            // ...
          }
          });
    

यहां दिया गया कोड, दूसरे चरण को रजिस्टर करने का पूरा उदाहरण दिखाता है:

Kotlin

val multiFactorAssertion = PhoneMultiFactorGenerator.getAssertion(credential)
user.multiFactor.session
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val multiFactorSession = task.result
            val phoneAuthOptions = PhoneAuthOptions.newBuilder()
                .setPhoneNumber(phoneNumber)
                .setTimeout(30L, TimeUnit.SECONDS)
                .setMultiFactorSession(multiFactorSession)
                .setCallbacks(callbacks)
                .build()
            // Send SMS verification code.
            PhoneAuthProvider.verifyPhoneNumber(phoneAuthOptions)
        }
    }

// Ask user for the verification code.
val credential = PhoneAuthProvider.getCredential(verificationId, verificationCode)

val multiFactorAssertion = PhoneMultiFactorGenerator.getAssertion(credential)

// Complete enrollment.
FirebaseAuth.getInstance()
    .currentUser
    ?.multiFactor
    ?.enroll(multiFactorAssertion, "My personal phone number")
    ?.addOnCompleteListener {
        // ...
    }

Java

MultiFactorAssertion multiFactorAssertion = PhoneMultiFactorGenerator.getAssertion(credential);
user.getMultiFactor().getSession()
  .addOnCompleteListener(
      new OnCompleteListener<MultiFactorSession>() {
      @Override
      public void onComplete(@NonNull Task<MultiFactorSession> task) {
        if (task.isSuccessful()) {
          MultiFactorSession multiFactorSession = task.getResult();
          PhoneAuthOptions phoneAuthOptions =
            PhoneAuthOptions.newBuilder()
                .setPhoneNumber(phoneNumber)
                .setTimeout(30L, TimeUnit.SECONDS)
                .setMultiFactorSession(multiFactorSession)
                .setCallbacks(callbacks)
                .build();
          // Send SMS verification code.
          PhoneAuthProvider.verifyPhoneNumber(phoneAuthOptions);
        }
      }
      });

// Ask user for the verification code.
PhoneAuthCredential credential =
  PhoneAuthProvider.getCredential(verificationId, verificationCode);

MultiFactorAssertion multiFactorAssertion = PhoneMultiFactorGenerator.getAssertion(credential);
// Complete enrollment.
FirebaseAuth.getInstance()
  .getCurrentUser()
  .getMultiFactor()
  .enroll(multiFactorAssertion, "My personal phone number")
  .addOnCompleteListener(
      new OnCompleteListener<Void>() {
      @Override
      public void onComplete(@NonNull Task<Void> task) {
        // ...
      }
      });

बधाई हो! आपने किसी उपयोगकर्ता के लिए, पुष्टि करने का दूसरा चरण रजिस्टर कर लिया है.

उपयोगकर्ताओं को दूसरे चरण की मदद से साइन इन कराना

मैसेज के ज़रिए दो चरणों में पुष्टि की सुविधा का इस्तेमाल करके, किसी उपयोगकर्ता को साइन इन कराने के लिए:

  1. उपयोगकर्ता को पहले चरण की मदद से साइन इन कराएं. इसके बाद, FirebaseAuthMultiFactorException गड़बड़ी को पकड़ें. इस गड़बड़ी में एक रिज़ॉल्वर होता है. इसका इस्तेमाल करके, उपयोगकर्ता के रजिस्टर किए गए दूसरे चरण की जानकारी पाई जा सकती है. इसमें एक ऐसा सेशन भी शामिल होता है जिससे यह साबित होता है कि उपयोगकर्ता ने पहले चरण की मदद से पुष्टि कर ली है.

    उदाहरण के लिए, अगर उपयोगकर्ता का पहला चरण, ईमेल और पासवर्ड था, तो:

    Kotlin

    FirebaseAuth.getInstance()
        .signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(
            OnCompleteListener { task ->
                if (task.isSuccessful) {
                    // User is not enrolled with a second factor and is successfully
                    // signed in.
                    // ...
                    return@OnCompleteListener
                }
                if (task.exception is FirebaseAuthMultiFactorException) {
                    // The user is a multi-factor user. Second factor challenge is
                    // required.
                    val multiFactorResolver =
                        (task.exception as FirebaseAuthMultiFactorException).resolver
                    // ...
                } else {
                    // Handle other errors, such as wrong password.
                }
            })
    

    Java

    FirebaseAuth.getInstance()
      .signInWithEmailAndPassword(email, password)
      .addOnCompleteListener(
          new OnCompleteListener<AuthResult>() {
          @Override
          public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
              // User is not enrolled with a second factor and is successfully
              // signed in.
              // ...
              return;
            }
            if (task.getException() instanceof FirebaseAuthMultiFactorException) {
              // The user is a multi-factor user. Second factor challenge is
              // required.
              MultiFactorResolver multiFactorResolver = task.getException().getResolver();
              // ...
            } else {
              // Handle other errors such as wrong password.
            }
          }
          });
    

    अगर उपयोगकर्ता का पहला चरण, OAuth जैसे फ़ेडरेटेड प्रोवाइडर से जुड़ा है, तो startActivityForSignInWithProvider() को कॉल करने के बाद, गड़बड़ी को पकड़ें.

  2. अगर उपयोगकर्ता ने पुष्टि करने के एक से ज़्यादा दूसरे चरण रजिस्टर किए हैं, तो उससे पूछें कि उसे कौनसे चरण का इस्तेमाल करना है:

    Kotlin

    // Ask user which second factor to use.
    // You can get the list of enrolled second factors using
    //   multiFactorResolver.hints
    
    // Check the selected factor:
    if (multiFactorResolver.hints[selectedIndex].factorId
        === PhoneMultiFactorGenerator.FACTOR_ID
    ) {
        // User selected a phone second factor.
        val selectedHint =
            multiFactorResolver.hints[selectedIndex] as PhoneMultiFactorInfo
    } else if (multiFactorResolver.hints[selectedIndex].factorId
        === TotpMultiFactorGenerator.FACTOR_ID) {
        // User selected a TOTP second factor.
    } else {
        // Unsupported second factor.
    }
    

    Java

    // Ask user which second factor to use.
    // You can get the masked phone number using
    // resolver.getHints().get(selectedIndex).getPhoneNumber()
    // You can get the display name using
    // resolver.getHints().get(selectedIndex).getDisplayName()
    if ( resolver.getHints()
                   .get(selectedIndex)
                   .getFactorId()
                   .equals( PhoneMultiFactorGenerator.FACTOR_ID ) ) {
    // User selected a phone second factor.
    MultiFactorInfo selectedHint =
      multiFactorResolver.getHints().get(selectedIndex);
    } else if ( resolver
                  .getHints()
                  .get(selectedIndex)
                  .getFactorId()
                  .equals(TotpMultiFactorGenerator.FACTOR_ID ) ) {
      // User selected a TOTP second factor.
    } else {
    // Unsupported second factor.
    }
    
  3. हिंट और दो चरणों में पुष्टि की सुविधा वाले सेशन के साथ, PhoneAuthOptions ऑब्जेक्ट शुरू करें. ये वैल्यू, FirebaseAuthMultiFactorException से जुड़े रिज़ॉल्वर में शामिल होती हैं.

    Kotlin

    val phoneAuthOptions = PhoneAuthOptions.newBuilder()
        .setMultiFactorHint(selectedHint)
        .setTimeout(30L, TimeUnit.SECONDS)
        .setMultiFactorSession(multiFactorResolver.session)
        .setCallbacks(callbacks) // Optionally disable instant verification.
        // .requireSmsValidation(true)
        .build()
    

    Java

    PhoneAuthOptions phoneAuthOptions =
      PhoneAuthOptions.newBuilder()
          .setMultiFactorHint(selectedHint)
          .setTimeout(30L, TimeUnit.SECONDS)
          .setMultiFactorSession(multiFactorResolver.getSession())
          .setCallbacks(callbacks)
          // Optionally disable instant verification.
          // .requireSmsValidation(true)
          .build();
    
  4. उपयोगकर्ता के फ़ोन पर, पुष्टि करने का मैसेज भेजें:

    Kotlin

    // Send SMS verification code
    PhoneAuthProvider.verifyPhoneNumber(phoneAuthOptions)
    

    Java

    // Send SMS verification code
    PhoneAuthProvider.verifyPhoneNumber(phoneAuthOptions);
    
  5. मैसेज के ज़रिए पुष्टि करने का कोड भेजने के बाद, उपयोगकर्ता से कोड की पुष्टि करने के लिए कहें:

    Kotlin

    // Ask user for the verification code. Then, pass it to getCredential:
    val credential =
        PhoneAuthProvider.getCredential(verificationId, verificationCode)
    

    Java

    // Ask user for the verification code. Then, pass it to getCredential:
    PhoneAuthCredential credential
        = PhoneAuthProvider.getCredential(verificationId, verificationCode);
    
  6. PhoneAuthCredential के साथ, MultiFactorAssertion ऑब्जेक्ट शुरू करें:

    Kotlin

    val multiFactorAssertion = PhoneMultiFactorGenerator.getAssertion(credential)
    

    Java

    MultiFactorAssertion multiFactorAssertion
        = PhoneMultiFactorGenerator.getAssertion(credential);
    
  7. पुष्टि करने के दूसरे चरण को पूरा करने के लिए, resolver.resolveSignIn() को कॉल करें. इसके बाद, साइन-इन के असली नतीजे को ऐक्सेस किया जा सकता है. इसमें, प्रोवाइडर के हिसाब से स्टैंडर्ड डेटा और पुष्टि करने के क्रेडेंशियल शामिल होते हैं:

    Kotlin

    multiFactorResolver
        .resolveSignIn(multiFactorAssertion)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val authResult = task.result
                // AuthResult will also contain the user, additionalUserInfo,
                // and an optional credential (null for email/password)
                // associated with the first factor sign-in.
    
                // For example, if the user signed in with Google as a first
                // factor, authResult.getAdditionalUserInfo() will contain data
                // related to Google provider that the user signed in with;
                // authResult.getCredential() will contain the Google OAuth
                //   credential;
                // authResult.getCredential().getAccessToken() will contain the
                //   Google OAuth access token;
                // authResult.getCredential().getIdToken() contains the Google
                //   OAuth ID token.
            }
        }
    

    Java

    multiFactorResolver
      .resolveSignIn(multiFactorAssertion)
      .addOnCompleteListener(
          new OnCompleteListener<AuthResult>() {
          @Override
          public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
              AuthResult authResult = task.getResult();
              // AuthResult will also contain the user, additionalUserInfo,
              // and an optional credential (null for email/password)
              // associated with the first factor sign-in.
              // For example, if the user signed in with Google as a first
              // factor, authResult.getAdditionalUserInfo() will contain data
              // related to Google provider that the user signed in with.
              // authResult.getCredential() will contain the Google OAuth
              // credential.
              // authResult.getCredential().getAccessToken() will contain the
              // Google OAuth access token.
              // authResult.getCredential().getIdToken() contains the Google
              // OAuth ID token.
            }
          }
          });
    

यहां दिया गया कोड, दो चरणों में पुष्टि की सुविधा का इस्तेमाल करने वाले उपयोगकर्ता को साइन इन कराने का पूरा उदाहरण दिखाता है:

Kotlin

FirebaseAuth.getInstance()
    .signInWithEmailAndPassword(email, password)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            // User is not enrolled with a second factor and is successfully
            // signed in.
            // ...
            return@addOnCompleteListener
        }
        if (task.exception is FirebaseAuthMultiFactorException) {
            val multiFactorResolver =
                (task.exception as FirebaseAuthMultiFactorException).resolver

            // Ask user which second factor to use. Then, get
            // the selected hint:
            val selectedHint =
                multiFactorResolver.hints[selectedIndex] as PhoneMultiFactorInfo

            // Send the SMS verification code.
            PhoneAuthProvider.verifyPhoneNumber(
                PhoneAuthOptions.newBuilder()
                    .setActivity(this)
                    .setMultiFactorSession(multiFactorResolver.session)
                    .setMultiFactorHint(selectedHint)
                    .setCallbacks(generateCallbacks())
                    .setTimeout(30L, TimeUnit.SECONDS)
                    .build()
            )

            // Ask user for the SMS verification code, then use it to get
            // a PhoneAuthCredential:
            val credential =
                PhoneAuthProvider.getCredential(verificationId, verificationCode)

            // Initialize a MultiFactorAssertion object with the
            // PhoneAuthCredential.
            val multiFactorAssertion: MultiFactorAssertion =
                PhoneMultiFactorGenerator.getAssertion(credential)

            // Complete sign-in.
            multiFactorResolver
                .resolveSignIn(multiFactorAssertion)
                .addOnCompleteListener { task ->
                    if (task.isSuccessful) {
                        // User successfully signed in with the
                        // second factor phone number.
                    }
                    // ...
                }
        } else {
            // Handle other errors such as wrong password.
        }
    }

Java

FirebaseAuth.getInstance()
  .signInWithEmailAndPassword(email, password)
  .addOnCompleteListener(
      new OnCompleteListener<AuthResult>() {
      @Override
      public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
          // User is not enrolled with a second factor and is successfully
          // signed in.
          // ...
          return;
        }
        if (task.getException() instanceof FirebaseAuthMultiFactorException) {
          FirebaseAuthMultiFactorException e =
            (FirebaseAuthMultiFactorException) task.getException();

          MultiFactorResolver multiFactorResolver = e.getResolver();

          // Ask user which second factor to use.
          MultiFactorInfo selectedHint =
            multiFactorResolver.getHints().get(selectedIndex);

          // Send the SMS verification code.
          PhoneAuthProvider.verifyPhoneNumber(
            PhoneAuthOptions.newBuilder()
                .setActivity(this)
                .setMultiFactorSession(multiFactorResolver.getSession())
                .setMultiFactorHint(selectedHint)
                .setCallbacks(generateCallbacks())
                .setTimeout(30L, TimeUnit.SECONDS)
                .build());

          // Ask user for the SMS verification code.
          PhoneAuthCredential credential =
            PhoneAuthProvider.getCredential(verificationId, verificationCode);

          // Initialize a MultiFactorAssertion object with the
          // PhoneAuthCredential.
          MultiFactorAssertion multiFactorAssertion =
            PhoneMultiFactorGenerator.getAssertion(credential);

          // Complete sign-in.
          multiFactorResolver
            .resolveSignIn(multiFactorAssertion)
            .addOnCompleteListener(
                new OnCompleteListener<AuthResult>() {
                  @Override
                  public void onComplete(@NonNull Task<AuthResult> task) {
                  if (task.isSuccessful()) {
                    // User successfully signed in with the
                    // second factor phone number.
                  }
                  // ...
                  }
                });
        } else {
          // Handle other errors such as wrong password.
        }
      }
      });

बधाई हो! आपने दो चरणों में पुष्टि की सुविधा का इस्तेमाल करके, किसी उपयोगकर्ता को साइन इन करा लिया है.

आगे क्या करना है