简体   繁体   中英

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() .
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
Homescreen() - View where the message about successful registration appears

You can first try to sign the user in with email using following code:

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

  • 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!

Good luck!


You can also check the Firebase docs for Email Verification

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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