onVerificationFailed функция вызывается каждый раз - PullRequest
0 голосов
/ 09 июля 2020

Я хочу выполнить аутентификацию по номеру телефона с помощью Firebase, но когда я пытаюсь запустить свое приложение на своем мобильном телефоне, отображается сообщение Toast Verification failed, а logcat не показывает никаких сообщений об ошибках. Я не знаю причины этого. Я уже прочитал много вопросов о переполнении стека, но они не решили мою проблему.

Вот мой код, в котором я получаю номер от пользователя

private var btn_go: Button? = null
private var et_phone_number: EditText? = null
private var auth: FirebaseAuth? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_phone)

    btn_go = findViewById(R.id.btn_go)
    et_phone_number = findViewById(R.id.et_phone_number)

    auth = FirebaseAuth.getInstance()

    btn_go?.setOnClickListener {
        enterno()
    }
}

// getting no
fun enterno() {

    val mobile: String = et_phone_number?.text.toString().trim()

    if (mobile.isEmpty() || mobile.length < 10) {
        Toast.makeText(applicationContext, "Enter a Valid Phone Number", Toast.LENGTH_SHORT)
            .show()
        return
    } else {

        val intent = Intent(this, OTP_Activity::class.java)

        intent.putExtra("mobile", mobile)
        startActivity(intent)
    }
}

Код, из которого я получаю otp

private var btn_signin: Button? = null

private var et_otp: EditText? = null
private var auth: FirebaseAuth? = null


private var mVerificationId: String? = null

//The edittext to input the code
private val editTextCode: EditText? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_o_t_p_)

    btn_signin = findViewById(R.id.btn_signin)
    et_otp = findViewById(R.id.et_otp)

    auth = FirebaseAuth.getInstance()

    val intent = intent
    val mobile = intent.getStringExtra("mobile")
    sendVerificationCode(mobile)

    btn_signin?.setOnClickListener {

        val co = et_otp!!.text.toString().trim { it <= ' ' }
        if (co.isEmpty() || co.length < 6) {
            Toast.makeText(applicationContext, "invalid code", Toast.LENGTH_LONG).show()
            et_otp!!.requestFocus()

        }
        else {
            verifyVerificationCode(co)
        }
    }
}

fun sendVerificationCode(mobile: String) {
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
        "+92$mobile",
        60,
        TimeUnit.SECONDS,
        this,
        mCallbacks
    )
}

//the callback to detect the verification status
private val mCallbacks: PhoneAuthProvider.OnVerificationStateChangedCallbacks =
    object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        override fun onVerificationCompleted(phoneAuthCredential: PhoneAuthCredential) {

            Toast.makeText(applicationContext, "onVerificationCompleted", Toast.LENGTH_LONG).show()
        }

        override fun onVerificationFailed(e: FirebaseException) {
            Toast.makeText(applicationContext, "verification failed", Toast.LENGTH_LONG).show()
        }
        override fun onCodeSent(
            s: String,
            forceResendingToken: PhoneAuthProvider.ForceResendingToken
        ) {
            super.onCodeSent(s, forceResendingToken)
            //storing the verification id that is sent to the user
            mVerificationId = s
        }
    }

private fun verifyVerificationCode(code: String) {
    //creating the credential
    val credential = PhoneAuthProvider.getCredential(mVerificationId!!, code)

    //signing the user
    signInWithPhoneAuthCredential(credential)
}

private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) {
    auth?.signInWithCredential(credential)
        ?.addOnCompleteListener(
            this@OTP_Activity,
            OnCompleteListener<AuthResult?> { task ->
                if (task.isSuccessful) {
                    //verification successful we will start the profile activity
                    Toast.makeText(applicationContext, "login successful", Toast.LENGTH_LONG)
                        .show()
                    val intent =
                        Intent(this, profileCreate::class.java)

                    startActivity(intent)
                } else {

                    //verification unsuccessful.. display an error message
                    Toast.makeText(
                        applicationContext,
                        "Somthing is wrong, we will fix it soon...",
                        Toast.LENGTH_LONG
                    ).show()

                    if (task.exception is FirebaseAuthInvalidCredentialsException) {
                        Toast.makeText(
                            applicationContext,
                            "Invalid code entered...",
                            Toast.LENGTH_LONG
                        ).show()
                    }
                }
            })
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...