我使用 firebase 电话身份验证创建了一个号码发送活动和确认/otp 片段。当定向到确认页面时,来自 Firebase 的 6 位短信代码会发送到输入的电话号码,但无论我做什么,输入的编辑文本和来自 Firebase 的代码都不匹配。 当我将编辑文本留空时,它会重定向到我想要的片段,就好像它是正确的一样。你能帮我看看我哪里出错了吗?我在确认片段中的代码如下;
类 FragmentRegisterTelOnay : Fragment() { var 来号 = "" Lateinit var auth : FirebaseAuth Lateinit var 回调:PhoneAuthProvider.OnVerificationStateChangedCallbacks var 验证ID = "" varcomingCode:字符串=“” 重写 fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? { var view = inflater.inflate(R.layout.fragment_register_activity_phone,container,false) view.tvKullaniciTelNo.setText("+90"+comingNumber) auth = Firebase.auth 设置回调()
view.ileriButton.setOnClickListener {
if (comingCode.equals(editTextOnayKodu.text.toString())){
EventBus.getDefault().postSticky(EventBusDataEvents.KayitBilgileriniGonder("+90$comingNumber",null,verificationID,comingCode))
val transaction = requireActivity().supportFragmentManager.beginTransaction()
transaction.replace(R.id.telefonOnayKod,FragmentRegisterDetailPhone())
transaction.addToBackStack("TelOnayfragmentEklendi")
transaction.commit()}
else{
Toast.makeText(activity,"Wrong Code",Toast.LENGTH_LONG).show()
}
}
val options = PhoneAuthOptions.newBuilder(auth)
.setPhoneNumber("+90"+comingNumber) // Phone number to verify
.setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
.setActivity(requireActivity()) // Activity (for callback binding)
.setCallbacks(callbacks) // OnVerificationStateChangedCallbacks
.build()
PhoneAuthProvider.verifyPhoneNumber(options)
return view
}
private fun setupCallBack() {
callbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(credential: PhoneAuthCredential) {
if(!credential.smsCode.isNullOrEmpty()){
comingCode = credential.smsCode!!
progressBarOnayKod.visibility = View.GONE
Log.e("Success","on verificationcompleted sms: " + comingCode)}
else{
Log.e("Error","onverification has not completed")
}
}
override fun onVerificationFailed(e: FirebaseException) {
Log.e("Error: ",e.localizedMessage)
progressBarOnayKod.visibility = View.GONE
}
override fun onCodeSent(verificationId: String,token: PhoneAuthProvider.ForceResendingToken) {
verificationID = verificationId
progressBarOnayKod.visibility = View.VISIBLE
Log.e("Codesent","oncodesent worked")
}
}
}
@Subscribe (sticky = true)
internal fun onTelefonEvent(kayitBilgileri: EventBusDataEvents.KayitBilgileriniGonder){
comingNumber = kayitBilgileri.telNo.toString()
Log.e("test",comingNumber)
}
override fun onAttach(context: Context) {
super.onAttach(context)
EventBus.getDefault().register(this)
}
override fun onDetach() {
super.onDetach()
EventBus.getDefault().unregister(this)
}
}
首先将 sha1 设置为 firebase 设置并生成 google config.json 然后添加到 poject 的根目录并添加到 build.gradle 依赖项。 它会正常工作
资源:https://github.com/firebase/quickstart-android/issues/283
根据官方Firebase文档(https://firebase.google.com/docs/auth/android/phone-auth?hl=en),使用短信代码进行身份验证的最后一步是使用signInWithCredential()方法,也许在它的帮助下您将能够解决您的问题。以下是 Firebase 官方文档中实现此方法的部分代码:
private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) {
auth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success")
val user = task.result?.user
} else {
// Sign in failed, display a message and update the UI
Log.w(TAG, "signInWithCredential:failure", task.exception)
if (task.exception is FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
}
// Update UI
}
} }