0
votes

How can I send a verification Email with the Firebase Verification Email template to an user when trying to create an account in Swift 3? Until now I have this code:

@IBAction func CreateAccount(_ sender: AnyObject) {

    let username = UserText.text
    let password = PasswordText.text

    FIRAuth.auth()?.createUser(withEmail: username!, password: password!, completion: { (user, error) in
        if error != nil {

            //error creating account
            let alert = UIAlertController(title: "Error", message: "Error creating account", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        } else {
            //success
            let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainVC")
            self.present(vc!, animated: true, completion: nil)
        }
    })
}

What I want is to confirm that the user is using a valid email.

2

2 Answers

0
votes

As the Firebase documentation on sending a password reset email shows:

You can send a password reset email to a user with the sendPasswordResetWithEmail:completion: method. For example:

FIRAuth.auth()?.sendPasswordReset(withEmail: userInput) { (error) in
  // ...
}
0
votes

don't know if you managed but I would use this:

final FirebaseUser user = mAuth.getCurrentUser();
user.sendEmailVerification()
    .addOnCompleteListener(this, new OnCompleteListener() {
        @Override
        public void onComplete(@NonNull Task task) {
            // Re-enable button
            findViewById(R.id.verify_email_button).setEnabled(true);

            if (task.isSuccessful()) {
                Toast.makeText(EmailPasswordActivity.this,
                         "Verification email sent to " + user.getEmail(),
                         Toast.LENGTH_SHORT).show();
             } else {
                 Log.e(TAG, "sendEmailVerification", task.getException());
                 Toast.makeText(EmailPasswordActivity.this,
                          "Failed to send verification email.",
                          Toast.LENGTH_SHORT).show();
             }
    }