-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataService.swift
executable file
·78 lines (59 loc) · 2.52 KB
/
DataService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Created by luis castillo on 3/22/17.
// Copyright © 2017 luis castill0. All rights reserved.
//
import Foundation
import Firebase
import FirebaseAuth
import FirebaseStorage
class DataService {
static let dataService = DataService()
private var _BASE_REF = FIRDatabase.database().reference()
var BASE_REF: FIRDatabaseReference {
return _BASE_REF
}
var currentUser: FIRUser? {
return FIRAuth.auth()?.currentUser
}
var storageRef: FIRStorageReference {
return FIRStorage.storage().reference()
}
var fileUrl: String!
func SignUp(username: String, email: String, password: String, data: Data) {
FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
if let error = error {
print(error.localizedDescription)
return
}
let filePath = "profileImage/\(user!.uid)"
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"
self.storageRef.child(filePath).put(data, metadata: metadata, completion: { (metadata, error) in
if let error = error {
print(error.localizedDescription)
return
}
//let seg = LoginViewController()
self.fileUrl = metadata!.downloadURLs![0].absoluteString
let changeRequestPhoto = user!.profileChangeRequest()
changeRequestPhoto.photoURL = URL(string: self.fileUrl)
changeRequestPhoto.commitChanges(completion: { (error) in
if let error = error {
print(error.localizedDescription)
} else {
let newUser = FIRDatabase.database().reference().child("users").child((user?.uid)!)
newUser.setValue(["displayName" : "\(username)", "email" : "\(email)", "id": "\(user!.uid)",
"profileURL": "\(self.fileUrl!)"])
}
})
})
let changeRequest = user?.profileChangeRequest()
changeRequest?.displayName = username
changeRequest?.commitChanges(completion: { (error) in
if let error = error {
print(error.localizedDescription)
return
}
})
})
}
}