在Firebase中创建用户后立即发送验证邮件

时间:2017-07-30 02:59:07

标签: ios swift firebase firebase-authentication

调用下面的代码时,我收到以下错误消息 "没有与此标识符对应的用户记录。用户可能已被删除。"
那个时候上面代码创建的用户不是吗? 我正在尝试在创建后使用验证电子邮件验证新用户的电子邮件。 感谢

let saveAction = UIAlertAction(title: "Create",
                                   style: .default) { action in
                                    let emailField = alert.textFields![0]
                                    let passwordField = alert.textFields![1]

                                    Auth.auth().createUser(withEmail: emailField.text!,
                                                           password: passwordField.text!) { user, error in
                                                            if error == nil {
                                                                Auth.auth().signIn(withEmail: self.textFieldLoginEmail.text!,
                                                                                   password: self.textFieldLoginPassword.text!)
                                                            }
                                                                                            }
                                    Auth.auth().currentUser?.sendEmailVerification { (error) in
                                      if let error = error
                                      {print("Error when sending Email verification is \(error)")}
                                    }

                                                        }

1 个答案:

答案 0 :(得分:1)

当您创建用户时,他们会自动登录。因此,您可以删除登录呼叫并将验证电子邮件的发送移至完成处理程序中:

Auth.auth().createUser(withEmail: emailField.text!,
                       password: passwordField.text!) { user, error in
                        if error == nil {
                          Auth.auth().currentUser?.sendEmailVerification { (error) in
                            if let error = error
                                ....

如果不能正常工作,登录方法也有一个完成处理程序,所以:

Auth.auth().signIn(withEmail: email, password: password) { (user, error) in

    if error == nil {
      Auth.auth().currentUser?.sendEmailVerification { (error) in
        if let error = error
        // ...
}