简体   繁体   English

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

[英]Send an email verfication email to a new Firebase User in Swift

I have my app set up to successfully login or create an account with Firebase using an email address and a password.我将我的应用程序设置为使用电子邮件地址和密码成功登录或创建 Firebase 帐户。 What I want to do now is to check if the user has verified their email, and if not send them the Verification email that Firebase allows us to write.我现在要做的是检查用户是否已经验证了他们的电子邮件,如果没有,则向他们发送 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()

        })

    }

}

Here are my "createaccount" and "login" functions.这是我的“创建帐户”和“登录”功能。

My problem is that I don't know how to implement this functionality, and where to call those functions.我的问题是我不知道如何实现这个功能,也不知道在哪里调用这些函数。

Can you show me what the function to send this email is, and what the function to check if the email is verified?你能告诉我发送这封电子邮件的功能是什么,以及检查电子邮件是否通过验证的功能是什么?

You can send the verification mail just after creating the account:您可以在创建帐户后立即发送验证邮件:

(Don't forget to create an additional button/action so the user may ask for the verification mail again) (不要忘记创建一个额外的按钮/操作,以便用户可以再次请求验证邮件)

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.
    }
}

Combining with your code:结合您的代码:

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

To check if the user has verified:检查用户是否已验证:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React Native 中向用户发送 Firebase 验证 email(以便用户可以验证他们的电子邮件)? - How do you send a Firebase verification email (so that a user can verify their email) to a user in React Native? 发送电子邮件到当前用户电子邮件 - send email to current user email in flutter Firebase 身份验证:通过 Email 查找用户 - Firebase Authentication : Lookup a user by Email 在 firebase 云函数 flutter 中发送计划 email - Send schedule email in firebase cloud functions flutter Firebase 通过 email 邀请用户并添加到公司 - Firebase Invite user by email and add to a company NullPointerException in send email验证,Android studio with google firebase - NullPointerException in send email verification, Android studio with google firebase 从身份验证列表中向 Firebase 上的所有用户发送 email - Send an email to all the users on Firebase from Authentication list 从发送验证邮件接收深度链接 (Flutter / Firebase) - Receive Deep Link from Send Verification Email (Flutter / Firebase) 我如何删除用户身份验证(电子邮件,密码)firebase angular - How i can delete a user auth (email , password ) firebase angular Firebase Email 链路认证 - Firebase Email Link Authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM