Firebase电子邮件验证无法验证帐户。

3

我检查了用户是否通过电子邮件验证,但是无论我发送和确认多少封电子邮件,验证状态仍然为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)
            }
        })

1
你在验证用户之后重新加载了currentUser吗? - Dravidian
你如何重新加载用户?终止应用程序不会吗?如果正在重新加载我的声誉,为什么它不能检测到用户已经验证了呢? - Tarvo Mäesepp
2
只有用户登录时,电子邮件验证属性才会重新加载。如果我没记错的话,您可以通过重新认证来获取更新后的值,但reloadWithCompletion也听起来很有前途。 - Frank van Puffelen
请参见https://dev59.com/jFoU5IYBdhLWcg3wCjrx。 - Frank van Puffelen
谢谢。搞定了 :) 我甚至没有想到需要做这样的事情。我以为 Firebase 会给用户添加一些标记,或者完全不同,因为文档对此非常模糊。即使我自己尝试查找这些值,如果我去查看文档,我也找不到它们 :D 每天都在学习新东西。 - Tarvo Mäesepp
2个回答

10

我实现这个功能的方法是添加一个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)

        }
    })

}

哇哦..真的可以工作了,太棒了:). 不过我应该把这个值保存到某个地方,这样我就不必每次点击按钮时都运行函数了吧?无论如何,非常感谢 :) - Tarvo Mäesepp
没有必要手动执行 reloadFIRAuth.auth()!.currentUser!.isEmailVerified,它们会自动处理。 - Dravidian

1
我的方法是添加一个 NSNotification.Name.UIApplicationDidBecomeActive,因为用户必须离开应用程序来验证电子邮件:
NotificationCenter.default.addObserver(self,selector:#selector(APEmailVerificationViewController.checkEmailVerificationState),name:NSNotification.Name.UIApplicationDidBecomeActive, object:  nil)

不要忘记在 viewDidDisappear 中移除通知。这是 APEmailVerificationViewController.checkEmailVerificationState 函数:
FIRAuth.auth()?.currentUser?.reload(completion: { [unowned self] (error) in
        if error != nil {
            print(error!.localizedDescription)
            return
        }
        if !FIRAuth.auth()!.currentUser!.isEmailVerified {
            return
        }

        /**
            Great! We go the playground of the app.
         */
        UIAlertView(title: "Hooray", message: "You've successfully verified your email", delegate: nil, cancelButtonTitle: "Ok").show()
        APIntent.gotoPlayground()
    })

希望它有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接