gpt4 book ai didi

swift - 如何允许用户使用 firebase 中的任何电子邮件地址进行访问?

转载 作者:可可西里 更新时间:2023-11-01 01:07:59 24 4
gpt4 key购买 nike

我想实现以下功能:

  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,该用户将在我的身份验证中创建,但我的数据库中没有条目。我也没有收到关于此问题的具体错误消息。

感谢您的帮助。

最佳答案

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

}

关于swift - 如何允许用户使用 firebase 中的任何电子邮件地址进行访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53498363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com