Firebase 电子邮件验证未按预期工作,未经验证用户无法登录

Firebase email verification not working as expected, without verification the user is able to login

我在使用 Firebase 及其电子邮件验证流程时遇到问题。我可以创建一个新用户,发送带有 link 的电子邮件,以验证电子邮件地址是否送达没有问题。现在,出于测试目的,我没有单击 link 来验证电子邮件,但是,如果我打开应用程序,我就可以访问并执行任何操作。我不确定我错过了什么或我做错了什么。在过去的几天里,我一直坚持这个。任何帮助是极大的赞赏。

我的代码

@IBAction func loginBtnTapped(_ sender: Any) {

    SVProgressHUD.show()
    guard let email = emailTxt.text,
        let password = passwordTxt.text else { return }

    Auth.auth().signIn(withEmail: email, password: password) { 
(user, error) in
        if error != nil {
            let alert = UIAlertController(title: "Login Error", 
message:"Incorrect Email and/or Password", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: 
.default) { _ in })
            self.present(alert, animated: true){}
            if let error = error {
                print("error: \(error.localizedDescription)")
            }
           if Auth.auth().currentUser?.isEmailVerified == false {
                let alert = UIAlertController(title: "Unable to 
login", message:"Pending: email verification", preferredStyle: 
.alert)
                alert.addAction(UIAlertAction(title: "OK", style: 
.default) { _ in })
                self.present(alert, animated: true){}
                print("")
            SVProgressHUD.dismiss()
            }
        }
            self.dismiss(animated: true, completion: nil)
            SVProgressHUD.dismiss()
        }
    }

预期结果

除非验证电子邮件,否则新创建的用户应该无法登录和打开应用程序。

在验证电子邮件地址之前,您应该禁用用户帐户。这似乎是安全禁止登录的唯一方法。

通常,您可以使用 sendSignInLinkToEmail() 发送带有特定 URL 的电子邮件地址验证消息。电子邮件验证过程后,用户将自动重定向到此 url。

在我们的案例中,我们邀请用户创建密码,然后在将他们重定向到登录屏幕之前激活他们的帐户。

Firebase 身份验证是关于验证 用户。如果您键入(说)正确的电子邮件地址和密码,我们相信您就是您。

如果您只想允许已验证其电子邮件地址的用户访问数据,这是可能的(也称为 授权)。您将在您尝试保护的后端进行检查,例如在您的 Firestore 数据库的安全规则中,如此处所示

另见

  • How to stop users from signing in without verifying e-mail I.D. first in Firebase?
  • Is it possible to enable Firebase email authentication but disable sign in?

您需要检查 firebase 数据库中显示 "is email verified" 的字段,然后如果该 BOOL 值为 TRUE,则让它们进入应用程序。在他们单击电子邮件中的 link 后,布尔值将自动变为 TRUE。因此,不要像您那样做,而是查询该用户的用户 table 并检查他们是否经过验证的布尔值,如果没有,则不要让他们进入。祝您好运,并有美好的一天

我能够按预期让它工作。用户需要验证电子邮件,否则,他们将无法访问该应用程序。我不必修改 Firebase 中的规则。 希望这对任何人都有帮助。

loginVC

private var authUser : User? {
        return Auth.auth().currentUser
    }
    public func verifyEmail() {
        authUser?.reload(completion: { (err) in
            if err == nil {
                if self.authUser!.isEmailVerified == true {
                   self.dismiss(animated: true, completion: nil)
                } else {
                    let alert = UIAlertController(title: "Confirm your email 
address.", message: "A confirmation email has been sent to" + "  " + 
((self.emailTxt.text)!) + " . " + "Click on the confirmation link to activate 
your account. ", preferredStyle: .alert)
                    let actionTaken = UIAlertAction(title: "OK", style: 
.default, handler: nil)
                    alert.addAction(actionTaken)
                    self.present(alert, animated: true, completion: nil)
                }
            }
        })
    }


    @IBAction func loginBtnTapped(_ sender: Any) {
        SVProgressHUD.show()
        guard let email = emailTxt.text,
            let password = passwordTxt.text else { return }

        Auth.auth().signIn(withEmail: email, password: password) { (user, 
error) in
            self.verifyEmail()
            if error != nil {
                let alert = UIAlertController(title: "Login Error", 
message:"Error: \(error!.localizedDescription)", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default) { _ 
in })
                self.present(alert, animated: true){}
                if let error = error {
                    print("error: \(error.localizedDescription)")
                }
            }
            SVProgressHUD.dismiss()
        }

    }