如何使用Firebase在Swift 3中发送验证电子邮件?

时间:2017-04-30 00:33:12

标签: xcode firebase swift3 firebase-authentication email-validation

在尝试在 Swift 3 中创建帐户时,如何向用户发送验证电子邮件 Firebase验证电子邮件模板?到目前为止,我有这段代码:

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

我想要的是确认用户使用的是有效的电子邮件。

2 个答案:

答案 0 :(得分:0)

正如Firebase documentation on sending a password reset email所示:

  

您可以使用sendPasswordResetWithEmail:completion:方法向用户发送密码重置电子邮件。例如:

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

答案 1 :(得分:0)

不知道你是否管理但我会用这个:

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();
             }
    }