Guest User

Untitled

a guest
Jan 18th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. import Foundation
  2. import Firebase
  3.  
  4. class DataService {
  5.  
  6. static let instance = DataService()
  7. private init() {}
  8.  
  9. public private(set) var REF_BASE = DB_BASE
  10. public private(set) var REF_USERS = DB_BASE.child("users")
  11. public private(set) var REF_DRIVERS = DB_BASE.child("drivers")
  12. public private(set) var REF_TRIPS = DB_BASE.child("trips")
  13.  
  14. func signupUser(email: String, password: String, segmentIndex: Int, imgFilename: String, uploadData: Data, vc: UIViewController, completion: @escaping CompletionHandler) {
  15. let isDriver = segmentIndex
  16. Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
  17. if let error = error {
  18. let alert = Alert()
  19. if let errCode = AuthErrorCode(rawValue: (error._code)) {
  20.  
  21. switch errCode {
  22. case .invalidEmail:
  23. print("Invalid email")
  24. alert.displayAlertMessage(alertTitle: "Invalid Email", messageToDisplay: "Email syntax is not correct", vc: vc)
  25. case .emailAlreadyInUse:
  26. print("Email already in use")
  27. alert.displayAlertMessage(alertTitle: "Email is taken", messageToDisplay: "This email is already in use", vc: vc)
  28. case .weakPassword:
  29. print("Password weak")
  30. alert.displayAlertMessage(alertTitle: "Weak Password", messageToDisplay: "Password is too weak. Please choose a password which contains at least 6 characters.", vc: vc)
  31. default:
  32. // ALWAYS GET HERE.
  33. print(error as Any)
  34. alert.displayAlertMessage(alertTitle: "Error", messageToDisplay: "An unknown error occured.", vc: vc)
  35. }
  36.  
  37. }
  38. } else {
  39. if let user = user {
  40. Storage.storage().reference().child("profile_image").child("\(imgFilename).jpg").putData(uploadData, metadata: nil, completion: { (metadata, error) in
  41. if let error = error {
  42. print("Failed to upload profile image:", error)
  43. return
  44. }
  45. guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else { return }
  46. print("Successfully uploaded profile image:", profileImageUrl)
  47.  
  48. if isDriver == 0 {
  49. let userData = ["provider": user.providerID, "userIsDriver": false, "profileImageUrl": profileImageUrl] as [String: Any]
  50. self.REF_USERS.child(user.uid).updateChildValues(userData)
  51. } else {
  52. let userData = ["provider": user.providerID, "userIsDriver": true, "profileImageUrl": profileImageUrl, "isPickUpModeEnabled": false, "driverIsOnTrip": false] as [String: Any]
  53. self.REF_DRIVERS.child(user.uid).updateChildValues(userData)
  54. }
  55. completion(true)
  56. })
  57. }
  58. }
  59. }
  60. }
  61. }
Add Comment
Please, Sign In to add comment