Firebase OTP验证onVerificationCompleted未调用 [英] Firebase OTP verification onVerificationCompleted not called
问题描述
我正在尝试设置OTP验证,因此,当用户输入他们的电话号码时,我会向他们发送一个个人识别码,并调用 onCodeSent()
并收到该个人识别码,但是问题是调用 onVerificationCompleted()
时,我想转到另一个活动,用户可以在其中输入代码图钉进行验证,但根本没有调用它,我也不明白为什么.任何帮助将不胜感激,谢谢.
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 when onVerificationCompleted()
is called, I would like to move to another activity where the user can enter the code pin to verify but it is not called at all and I don't understand why. Any help would be appreciated guys, thank you.
val auth = PhoneAuthOptions
.newBuilder(FirebaseAuth.getInstance())
.setPhoneNumber(phoneNumber)
.setTimeout(60L,TimeUnit.MILLISECONDS)
.setActivity(this)
.setCallbacks(object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(p0: PhoneAuthCredential) {
// here i want to get the smscode and send it over the next activity to verify
// but this method is not called at all
Intent(this,ChangePasswordActivity::class.java).apply {
putExtra("code",p0.smsCode)
startActivity(this)
}
}
override fun onVerificationFailed(p0: FirebaseException) {
Timber.d("Firebase Exception ${p0.message}")
}
override fun onCodeSent(code: String, p1: PhoneAuthProvider.ForceResendingToken) {
super.onCodeSent(code, p1)
}
override fun onCodeAutoRetrievalTimeOut(p0: String) {
super.onCodeAutoRetrievalTimeOut(p0)
}
})
.build()
PhoneAuthProvider.verifyPhoneNumber(auth)
推荐答案
onVerificationCompleted()
仅在电话号码经过验证且没有用户输入的情况下才被调用.要执行您尝试做的事情,您应该在 onCodeSent()
代替.
Here is a rough flow of events (that are covered in detail in the documentation):
- 从用户那里获取电话号码
- 致电
PhoneAuthProvider.verifyPhoneNumber(auth)
(已经存在)以将图钉发送给用户 调用 -
onCodeSent()
,并附带验证ID和重发令牌. - 在
onCodeSent()
内,创建一个旨在启动"pin输入屏幕"的意图.带有验证ID. - 从用户那里获取图钉,然后通过调用
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId,userInput)
将其与验证ID结合起来 - 使用该凭据通过
signInWithCredential(credential)
登录用户.
- Obtain phone number from user
- Call
PhoneAuthProvider.verifyPhoneNumber(auth)
(as you are already) to send the pin to the user onCodeSent()
is called, with the verification ID and a resending token.- Inside of
onCodeSent()
, create an intent to launch the "pin input screen" with the verification ID. - Get a pin from the user and then combine it with the verification ID by calling
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, userInput)
- 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() {
override fun onVerificationCompleted(credential: PhoneAuthCredential) {
// if here, phone number was verified automatically
mAuth.signInWithCredential(credential)
.addOnCompleteListener(/* ... */)
}
override fun onVerificationFailed(p0: FirebaseException) {
Timber.d("Firebase Exception ${p0.message}")
}
override fun onCodeSent(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)
这篇关于Firebase OTP验证onVerificationCompleted未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!