【问题标题】:SwiftUI, Firebase link to confirm EmailSwiftUI、Firebase 链接以确认电子邮件
【发布时间】:2020-08-03 16:49:56
【问题描述】:

我想增加在注册期间发送验证电子邮件的可能性。 如何在函数内部和 SignupView 中插入“sendEmailVerificationWithCompletion”?非常感谢:)

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")
            }
        }
    }
}

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-authentication swiftui


    【解决方案1】:

    您可以在该用户登录后随时将 Firebase 告知 send a verification email to the current user。例如,您可以在创建用户帐户后立即这样做:

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

    【讨论】:

    • 非常感谢 :) 我做到了。带有链接的电子邮件正确到达,即使它没有确认电子邮件,它也会登录。如果链接之前没有被确认,我如何添加一条规则说它不应该登录?
    • 这是不可能的,但也不需要。您可能想要的是当帐户未验证时应用程序不会继续运行,您可以通过检查用户的isEmailVerified 来做到这一点。然后,为了确保数据访问安全,您需要先检查服务器中的相同属性,然后再允许它们访问任何数据。这个话题之前已经讲过了,请看stackoverflow.com/a/56670780stackoverflow.com/a/48029049stackoverflow.com/a/55328819
    • 好的,我试试看:)再次感谢
    猜你喜欢
    • 2017-03-21
    • 2011-03-18
    • 1970-01-01
    • 2016-09-22
    • 2020-03-07
    • 1970-01-01
    • 2013-04-25
    • 2015-11-12
    • 2014-10-12
    相关资源
    最近更新 更多