0

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
killerwar557
  • 29
  • 1
  • 7

2 Answers2

0

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
  // ...
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • But the thing is that more than a password reset I want that the users can´t Sing In without checking their email first and verify it´s email address... Can you help me? Please! I have searched and I found out that is something like sendEmailVerification but I don't really know how to use that on my code... Thank you! – killerwar557 May 01 '17 at 03:30
  • You cannot prevent the user from signing in. But you can prevent them from accessing (for example) the Firebase Database by checking the `auth.token.email_verified` property in your security rules. See http://stackoverflow.com/a/42473081/209103 – Frank van Puffelen May 01 '17 at 04:04
0

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();
             }
    }
YakovL
  • 7,557
  • 12
  • 62
  • 102