Skip to content Skip to sidebar Skip to footer

Firebase Otp Verification Onverificationcompleted Not Called

I'm trying to set up OTP verification so when the user enters their phone number, I send them a pin code, the onCodeSent() is called and I receive the code pin, but the problem is

Solution 1:

onVerificationCompleted() will only be called when the phone number has been verified without any input from the user. To do what you are trying, you should be sending your intent inside onCodeSent() instead.

Here is a rough flow of events (that are covered in detail in the documentation):

  1. Obtain phone number from user
  2. Call PhoneAuthProvider.verifyPhoneNumber(auth) (as you are already) to send the pin to the user
  3. onCodeSent() is called, with the verification ID and a resending token.
  4. Inside of onCodeSent(), create an intent to launch the "pin input screen" with the verification ID.
  5. Get a pin from the user and then combine it with the verification ID by calling PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, userInput)
  6. Use that credential to sign in the user using signInWithCredential(credential).
val auth = PhoneAuthOptions
    .newBuilder(FirebaseAuth.getInstance())
    .setPhoneNumber(phoneNumber)
    .setTimeout(60L,TimeUnit.MILLISECONDS)
    .setActivity(this)
    .setCallbacks(object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        overridefunonVerificationCompleted(credential: PhoneAuthCredential) {
            // if here, phone number was verified automatically
            mAuth.signInWithCredential(credential)
                 .addOnCompleteListener(/* ... */)
        }

        overridefunonVerificationFailed(p0: FirebaseException) {
            Timber.d("Firebase Exception ${p0.message}")
        }

        overridefunonCodeSent(verificationId: String, resendToken: PhoneAuthProvider.ForceResendingToken) {
            // if here, code was sent to phone number// open pin input screen
            Intent(this,ChangePasswordActivity::class.java).apply {
                putExtra("verificationId",verificationId)
                startActivity(this)
            }
        }

        // we aren't using onCodeAutoRetrievalTimeOut, so it's omitted.
    })
    .build()

PhoneAuthProvider.verifyPhoneNumber(auth)

Solution 2:

I was also facing this problem but when I added my project to google cloud console, enable api services(google cloud console) and enable phone verification(google cloud console) then onVerificationCompleted is working fine. https://console.cloud.google.com/home

Post a Comment for "Firebase Otp Verification Onverificationcompleted Not Called"