一尘不染

Firebase电子邮件验证不验证帐户

swift

我检查了用户是否通过电子邮件验证。但是,无论我发送并确认了多少电子邮件,验证状态仍为false。检查时我做错什么了吗?

FIRAuth.auth()?.addStateDidChangeListener({ (auth, user) in
            if (auth.currentUser?.isEmailVerified)!{
                let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:nil)

                let NewPostViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "NewPostViewController")

                //Send the user to the LoginViewController
                self.present(NewPostViewController, animated: true, completion: nil)
            }else{
                let alertVC = UIAlertController(title: "Error", message: "Sorry. Your email address has not yet been verified. Do you want us to send another verification email to \(self.currentUser.generalDetails.email).", preferredStyle: .alert)
                let alertActionOkay = UIAlertAction(title: "Okay", style: .default) {
                    (_) in
                    FIRAuth.auth()?.currentUser?.sendEmailVerification(completion: nil)

                }
                let alertActionCancel = UIAlertAction(title: "Cancel", style: .default, handler: nil)
                alertVC.addAction(alertActionOkay)
                alertVC.addAction(alertActionCancel)
                self.present(alertVC, animated: true, completion: nil)
            }
        })

阅读 304

收藏
2020-07-07

共1个答案

一尘不染

我如何实现此功能的方法是添加一个 NSTimer 带有时间间隔的,它将检查用户是否已通过验证,然后在完成验证后终止计时器。

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)

        }
    })

}
2020-07-07