Guest User

Untitled

a guest
Feb 8th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. guard let image = profilePicture.image else { return }
  2. guard let password = passwordField.text else { return }
  3. guard let email = emailField.text else { return }
  4. // guard let username = usernameField.text else { return }
  5.  
  6.  
  7. SVProgressHUD.show()
  8. Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
  9. if error != nil{
  10. SVProgressHUD.dismiss()
  11. let alert = UIAlertController(title: "Error", message: "Please insert a valid email address and a password of at least 6 characters", preferredStyle: UIAlertController.Style.alert)
  12. alert.addAction(UIAlertAction(title: "Try again", style: UIAlertAction.Style.default, handler: nil))
  13. self.present(alert, animated: true, completion: nil)
  14. self.emailField.text = ""
  15. self.passwordField.text = ""
  16. }
  17.  
  18. if let user = Auth.auth().currentUser{
  19.  
  20. let imageName = NSUUID().uuidString
  21. let storageRef = Storage.storage().reference().child("(user.uid)/(imageName).jpeg")
  22.  
  23. if let uploadData = image.jpegData(compressionQuality: 0.75){
  24.  
  25. storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
  26. if error != nil{
  27. print(error!)
  28. return
  29. }
  30. print(metadata ?? "success")
  31.  
  32. storageRef.downloadURL(completion: { (url, error) in
  33. guard let downloadURL = url else {
  34. print("an error occured after uploading and then getting the URL")
  35. return
  36. }
  37.  
  38. let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
  39. changeRequest?.photoURL = downloadURL
  40. changeRequest?.commitChanges { (error) in
  41. //handle error
  42. }
  43.  
  44. })
  45.  
  46.  
  47. SVProgressHUD.dismiss()
  48. })
  49. }
  50. }
  51.  
  52.  
  53. // print("Registration succesful")
  54. // SVProgressHUD.dismiss()
  55.  
  56. }
  57.  
  58.  
  59. }
  60.  
  61. guard let uid = Auth.auth().currentUser?.uid else { return }
  62. let databaseRef = Database.database().reference().child("users/profile/(uid)")
  63.  
  64. let userObject = [
  65. "photoURL": profileImageURL.absoluteString
  66. ] as [String:Any]
  67.  
  68. databaseRef.setValue(userObject) { error, ref in
  69. completion(error == nil)
  70. }
  71. }
Add Comment
Please, Sign In to add comment