在 Swift 中向新的 Firebase 用户发送电子邮件验证电子邮件

Eog*_*sey 1 swift firebase-authentication

我已将我的应用设置为使用电子邮件地址和密码成功登录或创建 Firebase 帐户。我现在想要做的是检查用户是否已验证他们的电子邮件,如果没有,则向他们发送 Firebase 允许我们编写的验证电子邮件。

@IBAction func createAccountTapped(_ sender: Any) {

    if let email = emailTextfield.text, let password = passwordTextfield.text {

        Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in

            if let firebaseError = error {

                print(firebaseError.localizedDescription)

                return

            }

            self.presentTabBar()

        })

    }

}

@IBAction func loginTapped(_ sender: Any) {

    if let email = emailTextfield.text, let password = passwordTextfield.text {

        Auth.auth().signIn(withEmail: email, password: password, completion: {(user, error) in

            if let firebaseError = error {

                print(firebaseError.localizedDescription)

                return

            }

            self.presentTabBar()

        })

    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的“createaccount”和“login”功能。

我的问题是我不知道如何实现这个功能,也不知道在哪里调用这些函数。

Can you show me what the function to send this email is, and what the function to check if the email is verified?

EDU*_*sta 5

您可以在创建帐户后立即发送验证邮件:

(不要忘记创建一个额外的按钮/动作,以便用户可以再次要求验证邮件)

private var authUser : User? {
    return Auth.auth().currentUser
}

public func sendVerificationMail() {
    if self.authUser != nil && !self.authUser!.isEmailVerified {
        self.authUser!.sendEmailVerification(completion: { (error) in
            // Notify the user that the mail has sent or couldn't because of an error.
        })
    }
    else {
        // Either the user is not available, or the user is already verified.
    }
}
Run Code Online (Sandbox Code Playgroud)

结合您的代码:

@IBAction func createAccountTapped(_ sender: Any) {
    if let email = emailTextfield.text, let password = passwordTextfield.text {

        Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in

            if let firebaseError = error {
                print(firebaseError.localizedDescription)
                return
            }

            sendVerificationMail()


            self.presentTabBar()
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

要检查用户是否已验证:

@IBAction func loginTapped(_ sender: Any) {


    if let email = emailTextfield.text, let password = passwordTextfield.text {

        Auth.auth().signIn(withEmail: email, password: password, completion: {(user, error) in
            if let firebaseError = error {
                print(firebaseError.localizedDescription)
                return
            }

            if user != nil && !user!.isEmailVerified {
            // User is available, but their email is not verified.
            // Let the user know by an alert, preferably with an option to re-send the verification mail.
            }
            self.presentTabBar()
        })
    }
}
Run Code Online (Sandbox Code Playgroud)