简体   繁体   中英

SwiftUI, Firebase link to confirm Email

i would like to add the possibility to send a verification email during registration. How could i insert "sendEmailVerificationWithCompletion" inside the function and within the SignupView? Thanks a lot:)

import SwiftUI
import FirebaseAuth

// Add code here:
static func createUser(withEmail email:String, name: String, password:String, completionHandler:@escaping (Result<Bool,Error>) -> Void) {
    Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
        if let err = error {
            completionHandler(.failure(err))
            return
        }
        guard let _ = authResult?.user else {
            completionHandler(.failure(error!))
            return
        }
        let data = FBUser.dataDict(uid: authResult!.user.uid, name: name, email: authResult!.user.email!)
        
        FBFirestore.mergeFBUser(data, uid: authResult!.user.uid) { (result) in
            completionHandler(result)
        }
        completionHandler(.success(true))
    }
}

//And add code here:
struct SignUpView: View {
    var body: some View {
        VStack(spacing: 20 ) {
            Button(action: {
                FBAuth.createUser(withEmail: self.user.email, name: self.user.fullname, password: self.user.password) { (result) in
                    switch result {
                    case .failure(let error):
                        self.errorString = error.localizedDescription
                        self.showError = true
                    case .success(_):
                        print("Account creation successful")
                    }
                }
            }) {
                Text("Register")
            }
        }
    }
}

You can tell Firebase to send a verification email to the current user at any time after that user is signed in. For example, you could do so right after the user's account is created:

Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
    if let err = error {
        completionHandler(.failure(err))
        return
    }
    guard let _ = authResult?.user else {
        completionHandler(.failure(error!))
        return
    }

    // send verification email
    Auth.auth().currentUser?.sendEmailVerification { (error) in
      // ...
    }

    // write profile to database
    let data = FBUser.dataDict(uid: authResult!.user.uid, name: name, email: authResult!.user.email!)
    
    FBFirestore.mergeFBUser(data, uid: authResult!.user.uid) { (result) in
        completionHandler(result)
    }
    completionHandler(.success(true))
}

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