如何使用电子邮件将配置文件pic设置为firebase用户身份验证?

时间:2018-03-18 04:33:29

标签: swift3 firebase-realtime-database

我正在开发一个员工管理应用程序,这就是为什么我需要根据员工ID或电子邮件保存个人资料图片。在firebase数据库中是什么过程以及如何通过源代码设计数据库。在swift 3中,xcode 8.3.2, ios 10。

1 个答案:

答案 0 :(得分:0)

您需要将配置文件Image存储在firebase存储中, 之后将检索imageURL。检索到imageURL后,您需要将其作为userId的子项保存在firebase数据库中。 请参阅以下代码

func handleRegister() {
    guard let email = self.emailTextField.text, let password = self.passwordTextField.text, let name = self.nameTextField.text  else {
        print("Form is not valid")
        return
    }
    Auth.auth().createUser(withEmail: email, password: password) { (user: User?, error) in
        if error != nil {
            print(error!)
            return
        }
        guard let uid = user?.uid else {
            return
        }
        //success
        let imageName = NSUUID().uuidString
        let storageRef = Storage.storage().reference().child("\(imageName).png")
        if let uploadData = UIImageJPEGRepresentation(self.profileImageView.image!, 0.1) {
            storageRef.putData(uploadData, metadata: nil, completion:
                { (metadata, error) in
                    if  error != nil {
                        print(error!)
                        return
                    }
                    print(metadata!)
                    if let progileImageURL = metadata?.downloadURL()?.absoluteString {
                        let values = ["name": name, "email": email, "profileImageUrl": progileImageURL]
                        self.registerUserIntoDatabaseWithUID(uid: uid, values: values as [String : AnyObject])
                    }
            })
        }
    }
}


func registerUserIntoDatabaseWithUID(uid: String, values: [String: AnyObject]) {
    let ref = Database.database().reference()
    let userRef = ref.child("users").child(uid)
    userRef.updateChildValues(values, withCompletionBlock: { (err, ref) in
        if err != nil {
            print(err!)
            return
        }
        print("Saved user successfully into Firebase db")
        self.messagesController1?.checkIfUseLoggedin()
        self.dismiss(animated: true, completion: nil)
    })
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    var selectedImageFromPicker: UIImage?
    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
        selectedImageFromPicker = editedImage
        print(editedImage)
    }
    else if let orignalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
        selectedImageFromPicker = orignalImage
        print(orignalImage)
    }
    if let selectedImage = selectedImageFromPicker {
        self.profileImageView.image = selectedImage
    }
   self.dismiss(animated: true, completion: nil)
}


func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    print("picker cancled")
    self.dismiss(animated: true, completion: nil)
}