Auth.auth()。currentUser?.reload()不刷新currentUser.isEmailVerified

时间:2017-11-05 20:46:30

标签: swift firebase firebase-authentication

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

对此有何帮助?

3 个答案:

答案 0 :(得分:0)

您可以设置这样的计时器:

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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是电子邮件验证状态。如果电子邮件经过验证,它将为“ true”。注意! “ reload()”是异步的,“ b”将花一点时间才能获得“ true”。对我来说,不到半秒钟。