【问题标题】:How can I send a verification email in Swift 3 with Firebase?如何使用 Firebase 在 Swift 3 中发送验证电子邮件?
【发布时间】:2017-09-27 20:17:00
【问题描述】:

尝试在 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)
        }
    })
}

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 但问题是,我希望用户在不先检查他们的电子邮件并验证其电子邮件地址的情况下不能唱歌……你能帮帮我吗?请!我进行了搜索,发现类似于 sendEmailVerification 但我真的不知道如何在我的代码中使用它...谢谢!
    • 您无法阻止用户登录。但您可以通过检查安全规则中的auth.token.email_verified 属性来阻止他们访问(例如)Firebase 数据库。见stackoverflow.com/a/42473081/209103
    【解决方案2】:

    不知道你是否成功,但我会用这个:

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

    【讨论】:

    • 你测试过这个吗?据我所知,这段代码中至少缺少一个}
    • 这不是java吗?
    猜你喜欢
    • 2017-03-17
    • 2018-08-14
    • 2020-12-12
    • 2019-06-24
    • 2018-11-21
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 2019-03-13
    相关资源
    最近更新 更多