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

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

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

वेब कंटेनर इंस्टॉल करने से पहले

  1. बहु-स्तरीय पुष्टि (MFA) की सुविधा देने वाली कम से कम एक कंपनी चालू करें. फ़ोन से पुष्टि, पहचान छिपाकर पुष्टि करने, और Apple Game Center के अलावा, हर कंपनी MFA के साथ काम करती है.

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

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

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

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

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

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

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

बहु-स्तरीय पुष्टि (MFA) की सुविधा चालू करना

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

  2. बेहतर सेक्शन में, एसएमएस के लिए बहु-स्तरीय पुष्टि (MFA) चालू करें.

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

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

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

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

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

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

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

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

पुष्टि करने का दूसरा चरण रजिस्टर करना

किसी उपयोगकर्ता के लिए कोई नया सेकंडरी फ़ैक्टर रजिस्टर करने के लिए:

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

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

  3. उपयोगकर्ता के लिए एक से ज़्यादा तरीकों वाला सेशन पाएं:

    Kotlin+KTX

    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+KTX

    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+KTX

    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+KTX

    PhoneAuthProvider.verifyPhoneNumber(phoneAuthOptions)
    

    Java

    PhoneAuthProvider.verifyPhoneNumber(phoneAuthOptions);
    

    हालांकि ऐसा करना ज़रूरी नहीं है, लेकिन सबसे सही तरीका यह है कि आप उपयोगकर्ताओं को पहले ही यह बता दें कि उन्हें एसएमएस मिलेगा और सामान्य दरें लागू होंगी.

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

    Kotlin+KTX

    // 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. MultiFactorAssertion ऑब्जेक्ट को PhoneAuthCredential के साथ शुरू करें:

    Kotlin+KTX

    val multiFactorAssertion
      = PhoneMultiFactorGenerator.getAssertion(credential)
    

    Java

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

    Kotlin+KTX

    // 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+KTX

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+KTX

    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+KTX

    // 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+KTX

    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+KTX

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

    Java

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

    Kotlin+KTX

    // 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. MultiFactorAssertion ऑब्जेक्ट को PhoneAuthCredential के साथ शुरू करें:

    Kotlin+KTX

    val multiFactorAssertion = PhoneMultiFactorGenerator.getAssertion(credential)
    

    Java

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

    Kotlin+KTX

    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+KTX

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

बधाई हो! आपने बहु-स्तरीय पुष्टि (MFA) की सुविधा का इस्तेमाल करके एक उपयोगकर्ता खाते में साइन इन कर लिया है.

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