0

I am building a simple authentication app that authenticates the users through email/password and then through Phone. The layout is simple - User logs in with email/password in Login Activity and is then directed towards OTP Verification activity where OTP Verification takes place (and linking of the two separate user ids for email and phone).

The app completely runs fine for the test numbers in the Firebase. However, when I try it with real numbers it always throws the error saying The SMS Code has expired even though I enter the OTP as soon as I get it on my phone. It didn't happen at first.

I have already read answers about Auto-Retrieval feature of android and how it auto-verifies the OTP and hence when I enter the code manually, it has expired. I don't know much about authstatelistener and how to implement it. I did try this to turn off the auto retrieval for my number :

private fun disableAutoRetrievalForPhoneNumber() {
        Log.d(TAG,"Entered disableAutoRetrievalForPhoneNumber")
        val userId = FirebaseAuth.getInstance().currentUser?.uid
        userId?.let { uid ->
            val ref = db.collection("user").document(uid)
            ref.get()
                .addOnSuccessListener {
                    it?.data?.get("phone")?.toString()?.let { phoneNumber ->
                        Log.d(TAG, "Phone Number is $phoneNumber")
                        phoneNumber.let {
                            val firebaseAuthSettings: FirebaseAuthSettings =
                                auth.firebaseAuthSettings
                            firebaseAuthSettings.setAutoRetrievedSmsCodeForPhoneNumber(it, null)
                            Log.d(TAG,"Auto Retrieval Disabled")
                        }
                    }
                }

        }
    }

But no luck.

Is there a way to turn off this auto-retrieval feature and verify the OTP by entering it manually?

Or am I wrong about the whole auto-retrieval part and "The SMS Code has expired" error is due to something else entirely?

Please guide me.

0