简体   繁体   English

SwiftUI Firebase:发邮件到邮箱

[英]SwiftUI Firebase: Sending a message to the mail

In the function I want to add sending a message to the confirmation email, if you confirm, then you get to Homescreen() , else SignUp() .在 function 中我想添加发送消息到确认 email,如果你确认,那么你会进入Homescreen() ,否则SignUp()
What do I need to add to my code?我需要在我的代码中添加什么?

    func register(){
        
        if self.email != ""{
            if self.pass == self.repass{  
                Auth.auth().createUser(withEmail: self.email, password: self.pass) { (res, err) in
                    
                    if err != nil{   
                        self.error = err!.localizedDescription
                        self.alert.toggle()
                        return
                    }
                    
                    print("success")
                    
                    UserDefaults.standard.set(true, forKey: "status")
                    NotificationCenter.default.post(name: NSNotification.Name("status"), object: nil)
                }
            }
            else{   
                self.error = "Password mismatch"
                self.alert.toggle()
            }
        }
        else{  
            self.error = "Please fill all the contents properly"
            self.alert.toggle()
        }
    }

SignUp() - View where registration takes place SignUp() - 查看注册发生的位置
Homescreen() - View where the message about successful registration appears Homescreen() - 查看有关成功注册的消息出现的位置

You can first try to sign the user in with email using following code:您可以先尝试使用以下代码使用 email 登录用户:

Auth.auth().signIn(withEmail: email, link: self.link) { authResult, error in
      // ...
    }
  • If there is an error, present the error alert to the user.如果有错误,则向用户显示错误警报。
  • If sign in was successful, you can use the following code to get the current user:如果登录成功,您可以使用以下代码获取当前用户:
if let user = Auth.auth().currentUser {

}

And inside of the curly brackets just check if current user has already verified the E-Mail with user.isEmailVerified在花括号内检查当前用户是否已经使用user.isEmailVerified验证了电子邮件

  • If that is the case, then simply present your Homescreen如果是这种情况,则只需显示您的主屏幕

  • If current user has not verified his E-Mail yet, you can present an alert, where the user can choose if he wants to have the verification E-Mail sent out again.如果当前用户还没有验证他的电子邮件,你可以提出一个警告,用户可以选择是否要再次发送验证电子邮件。 If that is the case, you can simply resend the E-Mail with如果是这种情况,您可以简单地重新发送电子邮件

user.sendEmailVerification {error in
   
}

and the user will get his verification E-Mail or there can occur an error while sending the Email so you should handle that error aswell!并且用户将收到他的验证电子邮件,否则在发送 Email 时可能会发生错误,因此您也应该处理该错误!

Good luck!祝你好运!


You can also check the Firebase docs for Email Verification您还可以查看Firebase 文档以进行 Email 验证

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM