用于创建电话身份验证凭证的短信验证码无效

时间:2019-03-07 10:37:26

标签: java android firebase kotlin

在此处输入图片描述

如何修复用于创建电话身份验证凭证的短信验证码无效,请重新发送验证码短信,并确保使用Android Studio中用户提供的验证码

我的代码在kotlin中 如果您知道Kotlin或Java中的解决方案,请帮助我

我已在fierbase中添加了该应用,firebase正在发送验证码,但此错误正在显示!!

MainActivity:-

package com.khairo.firebasephoneauthentication

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import com.google.firebase.FirebaseApp
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

    FirebaseApp.initializeApp(this)
    val contry= arrayOf("JO", "SY","JA","USA")
    val contrynum= arrayOf("962", "963","123","1")
    spinnercon.adapter= ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, contry)

    continuee.setOnClickListener {

        val code=contrynum[spinnercon.selectedItemPosition]

        val number= mobile.text.toString().trim()

        if (number.isEmpty() || number.length< 9) {

            mobile.error = "Number is required"
            mobile.requestFocus()

            return@setOnClickListener
        }

        val phoneNumber= "+$code$number"

        val intent= Intent(this, VerifyPhoneActivity::class.java)
        intent.putExtra("phonenumber", phoneNumber)

        startActivity(intent)
    }
}
}

VerifyPhoneActivity:-

package com.khairo.firebasephoneauthentication

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.google.android.gms.tasks.TaskExecutors
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseException
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.PhoneAuthCredential
import com.google.firebase.auth.PhoneAuthProvider
import kotlinx.android.synthetic.main.activity_verify_phone.*
import java.util.concurrent.TimeUnit

class VerifyPhoneActivity : AppCompatActivity() {

private lateinit var varificationId: String
private lateinit var auth: FirebaseAuth

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_verify_phone)
    FirebaseApp.initializeApp(this)

    auth= FirebaseAuth.getInstance()

    val phonenumber= intent.getStringExtra("phonenumber")

    sendVeirficationCode(phonenumber)

    SignIn.setOnClickListener {
        val code= textCode.toString().trim()

        if (code.isEmpty() || code.length< 6) {
            textCode.error= "Enter code..."
            textCode.requestFocus()
            return@setOnClickListener
        }

        progressbar.visibility= View.VISIBLE
        verfyCode(code)
    }
}

private fun verfyCode(code: String) {

    val credential: PhoneAuthCredential= PhoneAuthProvider.getCredential(varificationId, code)

    singInWithCredential(credential)
}

private fun singInWithCredential(credential: PhoneAuthCredential) {

    auth.signInWithCredential(credential)
        .addOnCompleteListener { task ->

            if (task.isSuccessful) {
                val intent= Intent(this, ProfileActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

                startActivity(intent)

            } else {

                errrror.setText(task.exception!!.message)
                Toast.makeText(this, task.exception!!.message, Toast.LENGTH_LONG).show()
            }
        }
}

private fun sendVeirficationCode(number: String) {

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
        number,
        60,
        TimeUnit.SECONDS,
        TaskExecutors.MAIN_THREAD,
        CallBack()
    )
}

private fun CallBack() = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

    override fun onCodeSent(p0: String, p1: PhoneAuthProvider.ForceResendingToken?) {
        super.onCodeSent(p0, p1)

        varificationId= p0
    }
    override fun onVerificationCompleted(p0: PhoneAuthCredential) {

        val code= p0.smsCode

        if (code!= null) {
            progressbar.visibility= View.VISIBLE
            verfyCode(code)
        }
    }

    override fun onVerificationFailed(p0: FirebaseException) {

        errrror.setText(p0.message)
        Toast.makeText(this@VerifyPhoneActivity, p0.message, Toast.LENGTH_LONG).show()
    }

}
}

0 个答案:

没有答案