【问题标题】:Firebase OTP verification onVerificationCompleted not calledFirebase OTP 验证 onVerificationCompleted 未调用
【发布时间】:2021-04-07 16:24:50
【问题描述】:

我正在尝试设置 OTP 验证,因此当用户输入他们的电话号码时,我向他们发送一个 pin 码,onCodeSent() 被调用并且我收到密码 pin,但问题是当 onVerificationCompleted() 是调用,我想转到另一个活动,用户可以在其中输入代码 pin 进行验证,但根本没有调用它,我不明白为什么。任何帮助将不胜感激,谢谢。

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)

【问题讨论】:

    标签: android kotlin firebase-authentication one-time-password


    【解决方案1】:

    onVerificationCompleted() 只会在电话号码经过验证且用户没有任何输入的情况下才会被调用。要执行您正在尝试的操作,您应该改为在 onCodeSent() 内发送您的意图。

    这是一个粗略的事件流程(在documentation 中有详细介绍):

    1. 从用户那里获取电话号码
    2. 致电 PhoneAuthProvider.verifyPhoneNumber(auth)(您已经这样做了)将 pin 发送给用户
    3. onCodeSent() 被调用,带有验证 ID 和重新发送令牌。
    4. onCodeSent() 内部,创建一个意图以使用验证 ID 启动“pin 输入屏幕”。
    5. 从用户那里得到一个pin,然后通过调用PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, userInput)将它与验证ID结合起来
    6. 使用该凭据通过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)
    

    【讨论】:

    • 我想要实现的想法是我想比较两个密码,因为我不是用它来登录用户,而是在他的号码中获得密码后恢复他的密码,在这种情况下验证 id 与那个 pin 码不同,我希望有人获得发送给用户的相同密码 pin 并将其发送到 Intent extras 并且非常在另一个屏幕上,是那个人还是我的方法不好谢谢?
    • 您永远不会被告知原始密码,因为这违背了越界身份验证的目的。您只会收到您随用户输入发送的验证 ID。您也无法“恢复”用户的密码,只能重置/更改它 - 您永远不应该将用户的密码存储在任何地方。要更改用户的密码,您应该可以使用上述凭据登录,然后使用updatePassword()(假设您已关联帐户)。
    • 假设用户忘记了他的密码并且无法再访问他的帐户,在这种情况下 OTP 不值得,我宁愿参考电子邮件验证来重置密码,恢复我的意思是重置为好吧,那还可以吗,谢谢?
    • 感谢您分享链接,因此总结起来主要使用 otp 重置用户密码是不可能的
    【解决方案2】:

    我也遇到了这个问题,但是当我将我的项目添加到 google cloud console 时,启用 api 服务(google cloud console)并启用电话验证(google cloud console) 然后 onVerificationCompleted 工作正常。 https://console.cloud.google.com/home

    【讨论】:

      猜你喜欢
      • 2022-12-28
      • 2020-05-11
      • 1970-01-01
      • 2019-12-01
      • 2022-06-11
      • 1970-01-01
      • 2020-03-11
      • 2018-08-08
      • 1970-01-01
      相关资源
      最近更新 更多