【问题标题】:Auth.auth().currentUser?.reload() doesn't refresh currentUser.isEmailVerifiedAuth.auth().currentUser?.reload() 不刷新 currentUser.isEmailVerified
【发布时间】:2017-11-05 20:46:29
【问题描述】:

我正在尝试使用 Firebase 实施电子邮件验证。我已创建成功重定向到我的应用的动态链接。我也测试了网络上的链接。它运行良好,并且可以验证电子邮件。但是,验证电子邮件上的链接将我重定向到我的应用程序,Auth.auth().currentUser.isEmailVerified 仍然给我错误,即使我事先运行了 Auth.auth().currentUser?.reload() 命令。

有什么帮助吗?

【问题讨论】:

  • 在应用操作代码后在相应用户上调用 reload 应将用户上的 emailVerified 更新为 true。如果这没有发生,您需要向 Firebase 支持提交错误报告。
  • @bojeil,“动作代码”是什么意思?
  • 这是通过链接发送的代码。它通过oobCode=actionCode 附加到链接中。您解析该代码并将其传递给auth.applyActionCode 以兑换它。

标签: swift firebase firebase-authentication


【解决方案1】:

您可以这样设置计时器:

var verificationTimer : Timer = Timer()    // Timer's  Global declaration

self.verificationTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(LoginViewController.checkIfTheEmailIsVerified) , userInfo: nil, repeats: true)

然后使用该函数反复检查用户当前状态:

func checkIfTheEmailIsVerified(){

    FIRAuth.auth()?.currentUser?.reload(completion: { (err) in
        if err == nil{

            if FIRAuth.auth()!.currentUser!.isEmailVerified{

                let feedVCScene = self.navigationController?.storyboard?.instantiateViewController(withIdentifier: "ViewControllerVC_ID") as! ViewController
                self.verificationTimer.invalidate()     //Kill the timer
                self.navigationController?.pushViewController(feedVCScene, animated: true)
                // Segueing to the next view(i prefer the instantiation method).
            } else {

                print("It aint verified yet")

            }
        } else {

            print(err?.localizedDescription)

        }
    })

}

希望这会有所帮助:)

来源:https://stackoverflow.com/a/40037547/6596799

【讨论】:

  • Anushk 令人惊讶的是它起作用了。但是你能解释一下为什么它适用于重复计时器而不适用于只执行一次用户重新加载吗?
  • 现在它不工作了。我不确定发生了什么
  • 您想不断检查电子邮件是否经过验证。太奇怪了,能不能在代码中尝试下断点来检查验证,看看有没有被调用?
  • 它正在调用该函数来检查电子邮件是否经过验证。但是,我让它迭代了 1 分钟,即 60 次迭代,仍然没有工作。
【解决方案2】:

.reload() 是异步的,因此您可以尝试使用.then 检查isEmailVerified

Auth.auth().currentUser?.reload()
  .then(() => console.log(Auth.auth().currentUser.isEmailVerified)

【讨论】:

    【解决方案3】:

    Kotlin AndroidStudio 中,我使用了一个按钮“验证电子邮件!” enter image description here

    类:

    private var b = false //verificacion email

    按钮

    btnVerify.setOnClickListener{
                verifyEmail()
            }
    

    然后

    private fun verifyEmail(){
            val mAuth = FirebaseAuth.getInstance()
            mAuth.currentUser?.reload()?.addOnCompleteListener {
                b = mAuth.currentUser!!.isEmailVerified
                Log.d("VERI","state: $b")
                if(b){
                    btnVerify.visibility = View.GONE
                    yuorButton.visibility = View.VISIBLE //optional
                }else{
                    Toast.makeText(applicationContext,"your email not verified", Toast.LENGTH_SHORT).show()
                }
            }
    }
    

    b 是电子邮件验证状态。如果电子邮件经过验证,它只会是“真实的”。注意力! “reload()”是异步的,“b”需要一点时间才能得到“true”。对我来说不到半秒。

    【讨论】:

      猜你喜欢
      • 2020-04-08
      • 2019-04-20
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多