即使电子邮件格式不正确,validate总是返回true

时间:2020-05-10 10:25:00

标签: swift

  func createUser(_ email: String, _ password: String) -> Bool
    {

        var validate = true 

        Auth.auth().createUser(withEmail: email, password: password)
        {
            (authResult, error) in

            if error != nil
            {
                validate = false
            }
        }

        return validate

    }
//validate email and register using registercontroller
        let canRegister = RC.createUser(email!, password!)

        var message = ""

        if canRegister
        {
            message = "Welcome \(name)"

            let alert = UIAlertController(title: "Register Success", message: message, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Back", style: .default, handler:
            {   Void in
                self.dismiss(animated: true, completion: nil)

            }))
            self.present(alert,animated:true, completion:nil)
            print(String(canRegister))
            return
        }

        else
        {
            lblErrorText.text = "Invalid email format or email already existed"
            print(String(canRegister))
            return
        }

我正在做一个简单的firebase注册过程,但是即使电子邮件格式不正确,validate也将始终返回true

validate return true although email format is incorrect

尽管它不会注册到Firebase中。如果存在错误,我希望它返回false。

1 个答案:

答案 0 :(得分:1)

创建用户是一种异步方法

func createUser(_ email: String, _ password: String,completion:@escaping(Bool -> ())) { 

    Auth.auth().createUser(withEmail: email, password: password)
    {
        (authResult, error) in

         completion(error == nil)

    } 

}

然后

RC.createUser(email!, password!) { canRegister in 

    if canRegister
    {
        message = "Welcome \(name)"

        let alert = UIAlertController(title: "Register Success", message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Back", style: .default, handler:
        {   Void in
            self.dismiss(animated: true, completion: nil)

        }))
        self.present(alert,animated:true, completion:nil)
        print(String(canRegister))
        return
    }

    else
    {
        lblErrorText.text = "Invalid email format or email already existed"
        print(String(canRegister))
        return
    }
 }
相关问题