如果电子邮件未经验证,如何不让用户注册 [英] How to not let the user signup if the email isn’t verified

查看:58
本文介绍了如果电子邮件未经验证,如何不让用户注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个基本的应用程序,可让用户注册和登录.这是允许用户注册的代码,它将用户添加到数据库中.一旦用户单击(轻击)注册按钮,就会向他们发送电子邮件验证"电子邮件.

I have made a basic app that lets users to signup and login. Here is the code to let the user sign up and it adds the user to the database. Once the user clicks(taps) on the signup button, an 'email verification' email will be sent to them.

var verificationTimer = Timer()

@objc func signupButtonPressed() {

    // Check that the text fields aren't empty
    if name.text!.isEmpty || email.text!.isEmpty || password.text!.isEmpty || countryTextField.text!.isEmpty {

        let emptyFieldAlert = UIAlertController(title: "Error", message: "Please fill out all the fields.", preferredStyle: .alert)
        emptyFieldAlert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
        self.present(emptyFieldAlert, animated: true)

    } else { // If none of the text fields are empty

        // Create a user
        func createUser(withEmail email: String, name: String, password: String, country: String) {

            // Sign up user only if all the text fields are filled
            Auth.auth().createUser(withEmail: email, password: password) { (result, error) in

                if result != nil {

                    // Send an 'email verification' email to the user
                    Auth.auth().currentUser?.sendEmailVerification(completion: nil)

                    // Set a timer to call the emailverification function every second
                    self.verificationTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.checkIfEmailIsVerified), userInfo: nil, repeats: true)

                } else {

                    // Error: show error message
                    print("Failed to sign user up with error: ", error!.localizedDescription)
                    let emptyFieldAlert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
                    emptyFieldAlert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
                    self.present(emptyFieldAlert, animated: true)

                    return
                }}}


        createUser(withEmail: self.email.text!, name: self.name.text!, password: self.password.text!, country: self.countryTextField.text!)

我创建了一个计时器,该计时器每秒调用一次函数,该函数检查用户是否已验证其电子邮件.并且,如果用户已经验证了他们的电子邮件,计时器将停止运行,并将用户定向到该应用程序的主页.我给了下面的代码.注意:无论电子邮件验证如何,都会创建一个用户帐户.

I have created a timer that calls a function every second, the function checks if the user has verified their email. And if the user has verified their email, the timer will stop and the user will be directed to the main page of the app. I've given the code below. NOTE: A user account will be created regardless of the verification of the email.

// Check if the user has verified the email
@objc func checkIfEmailIsVerified() {
    Auth.auth().currentUser?.reload(completion: {(error) in
        if error == nil {

            if Auth.auth().currentUser!.isEmailVerified {

                // Stop the timer
                self.verificationTimer.invalidate()
                print(self.verificationTimer)

                // User verified, go to main page
                let mainPage = MainScreen()
                self.present(mainPage, animated: true, completion: nil)

            }
        }
    })
} 

我的主页上还有一个功能,可以使用户保持登录状态,除非他们已经注销.这是该代码:

I also have a function on my main page that keeps the user logged in unless they have logged out. Here is the code for that:

// Check if the user is signed in
func authenticateUserAndConfigure() {

    if Auth.auth().currentUser == nil {
        DispatchQueue.main.async {
            // If user isn't singed in, user will be directed to the login page
            self.present(LoginScreen(), animated: false, completion: nil)
        }
    }
}

这几乎可以完美地工作,问题是,无论电子邮件验证如何,都会创建一个用户,如果用户在不验证电子邮件的情况下单击(点击)注册后退出了该应用程序并重新启动它,则该用户将被定向到主页(最后一个代码段).因此,基本上,我希望用户仅在验证电子邮件后才将其添加到数据库中.

This works almost perfectly, the problem is, a user is created regardless of the email verification, if the user quits the app and relaunches it after clicking (tapping) on sign up without verifying their email, the user will be directed to the main page (last code snippet). So basically, I want the user to get added to the database only after the user verifies their email.

推荐答案

我知道了,这是代码:

// Check if the user is signed in
func authenticateUserAndConfigure() {

    if Auth.auth().currentUser == nil {
        DispatchQueue.main.async {
            // If user isn't singed in, user will be directed to the login page
            self.present(LoginScreen(), animated: false, completion: nil)
        }
    } else {

        if Auth.auth().currentUser?.isEmailVerified == false {

            DispatchQueue.main.async {
                // If user hasn't verified their email, the user will be directed to the login page
                self.present(LoginScreen(), animated: false, completion: nil)
            }

        }
    }
}

这将检查用户是否已验证他们的电子邮件,如果尚未验证,他们将被注销.

This checks if the user has verified their email and if they haven't, they will be logged out.

这篇关于如果电子邮件未经验证,如何不让用户注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆