如何在Firebase中允许用户访问任何电子邮件地址?

时间:2018-11-27 11:10:22

标签: swift firebase

我要实现以下功能:

  1. 通过我的应用程序注册,并使用以下所有可能的电子邮件地址 以下格式,例如xx @ xx.com,xx.xx @ x-xx.de,xx-xx @ xx-xx.com ...

我在Firebase中启用了电子邮件/密码验证,并具有以下代码来创建用户:

     @IBAction func createButtonTapped(_ sender: UIButton) {
    view.endEditing(true)

    guard let image = selectedImage else { return }
    guard let imageData = image.jpegData(compressionQuality: 0.1) else { return }
    ProgressHUD.show("Lade", interaction: false)

    if manager.location != nil {
        // Aktuellen Standort laden
        let userLat = UserDefaults.standard.value(forKey: "current_latitude") as! String
        let userLong = UserDefaults.standard.value(forKey: "current_longitude") as! String

        AuthService.createUser(username: usernameTextfield.text!, email: emailtextfield.text!, password: passwortTextField.text!, imageData: imageData, onSuccess: {

            let location:CLLocation = CLLocation(latitude: CLLocationDegrees(Double(userLat)!), longitude: CLLocationDegrees(Double(userLong)!))
            // Update aktuellen Standort
            LocationApi.shared.locationManager(self.manager, didUpdateLocations: [location])

            ProgressHUD.showSuccess("Profil wurde erstellt")
            self.performSegue(withIdentifier: "registerSegue", sender: nil)
        }) { (error) in
            ProgressHUD.showError("User konnte nicht erstellt werden")
        }
    } else {
        ProgressHUD.dismiss()
        self.performSegue(withIdentifier: "RegistrationToErrorVc", sender: nil)
    }


}

    // Account erstellen
static func createUser(username: String, email: String, password: String, imageData: Data, onSuccess: @escaping () -> Void, onError: @escaping (_ error: String?) -> Void) {

    Auth.auth().createUser(withEmail: email, password: password) { (data, error) in
        if let err = error {
            onError(err.localizedDescription)
            return
        }
        // User erfolgreich erstellt
        guard let uid = data?.user.uid else { return }
        self.uploadUserData(uid: uid, username: username, email: email, imageData: imageData, onSuccess: onSuccess)
    }
}





static func uploadUserData(uid: String, username: String, email: String, imageData: Data, onSuccess: @escaping () -> Void) {


    let storageRef = Storage.storage().reference().child("profile_image").child(uid)

    storageRef.putData(imageData, metadata: nil) { (metadata, error) in
        if error != nil {
            return
        }

    storageRef.downloadURL(completion:  { (url, error) in
        if  error != nil {
            print(error!.localizedDescription)
            return
        }

        let profilImageURL = url?.absoluteString


        let ref = Database.database().reference().child("users").child(uid)
        ref.setValue(["uid": uid, "username" : username,"username_lowercase": username.lowercased(),"radius": "20", "email" : email, "profileImageURL": profilImageURL ?? "Kein Bild vorhanden"])
    })
     onSuccess()
    }
}

当我添加一个用户(例如p.m@xx-xx.com)时,将在我的身份验证中创建该用户,但是在我的数据库中没有任何条目。对于此问题,我也没有得到任何具体的错误消息。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

storageRef.putDatastorageRef.downloadURL都是异步函数。我之所以对您的setValue为何未执行的猜测是因为storageRef.downloadURL尚未实际上传数据,因此storageRef.putData返回了错误。

我建议像这样通过metaData检索上传的URL:

guard let imageData = UIImageJPEGRepresentation(image, 0.75) else { return }
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
storageRef.putData(imageData, metadata: metaData) { metaData, error in
    if error == nil, metaData != nil {
        // upload was successful
        if let url = metaData?.downloadURL() {
            ref.setValue(["uid": uid, "username" : username,"username_lowercase": username.lowercased(),"radius": "20", "email" : email, "profileImageURL": url ?? "Kein Bild vorhanden"])
            onSuccess()
        } else {
            onSuccess()
        }
    } else {
        // error handle - your completion handler should really return a bool so you know if the upload was successful or not
        onSuccess()
    }

}