user2867432
user2867432

Reputation: 382

Sending a verification email right after user creation in Firebase

When calling the code below I get the following error message "There is no user record corresponding to this identifier. The user may have been deleted."
Isn't the user created by the code above at that point? I am trying to validate the new user's email using a verification email after its creation. Thanks

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)")}
                                    }

                                                        }

Upvotes: 0

Views: 809

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

When you create a user, they're automatically signed in. So you can remove the sign-in call and move the sending of the verification email into the completion handler:

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
                                ....

In cases where that won't work, the sign-in method also has a completion handler, so:

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

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

Upvotes: 1

Related Questions