Repository navigation
Expand file tree
/
Copy pathDatabaseAccessExample_02.swift
More file actions
168 lines (142 loc) · 6.03 KB
/
Copy pathDatabaseAccessExample_02.swift
File metadata and controls
168 lines (142 loc) · 6.03 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Code by Ethan Palosh for the iOS App "Detailer"
DatabaseAccessExample_02 - User authorization & account management
This file contains some sample high-level database access functions. They are implemented in the app code.
- This class specifically handles authorization in the app. Signing users in/out, and handling user data.
Published for portfolio purposes only.
*/
import Foundation
import FirebaseAuth
import FirebaseFirestore
class AuthService {
static let shared = AuthService()
private init() {}
// function to sign in from user input
func signIn(email: String, password: String, completion: @escaping (Result<User, Error>) -> Void) {
Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
if let error = error {
completion(.failure(error))
} else if let user = authResult?.user {
ConnectionViewModel.shared.reset()
completion(.success(user))
}
}
}
// function to create a new user based on user input
func signUp(email: String, password: String, firstName: String, lastName: String, schoolName: String?, phoneNumber: String, birthday: Date?, userType: String?, completion: @escaping (Result<User, Error>) -> Void) {
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if let error = error {
completion(.failure(error))
} else if let user = authResult?.user {
// Construct user data to save to Firestore
var userData: [String: Any]
if let birthday = birthday {
userData = [
"email": email,
"uid": user.uid,
"firstName": firstName,
"lastName": lastName,
"phoneNumber": phoneNumber,
"birthday": birthday
]
}
else {
userData = [
"email": email,
"uid": user.uid,
"firstName": firstName,
"lastName": lastName,
"phoneNumber": phoneNumber
]
}
if let schoolName = schoolName {
userData["schoolName"] = schoolName
}
if let userType = userType {
userData["userType"] = userType
}
// Save user data to Firestore
FirestoreService.shared.saveUserData(userId: user.uid, data: userData) { result in
switch result {
case .success:
// Optionally save additional user details to other collections if needed
FirestoreService.shared.saveUserWithEmailAsID(email: email, uid: user.uid) { result in
switch result {
case .success:
completion(.success(user))
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
}
// function to sign the current user out
func signOut(completion: @escaping (Result<Void, Error>) -> Void) {
do {
try Auth.auth().signOut()
completion(.success(()))
} catch {
completion(.failure(error))
}
}
// Function to delete the current user account
func deleteAccount(completion: @escaping (Result<Void, Error>) -> Void) {
guard let user = Auth.auth().currentUser else {
completion(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "No user is signed in."])))
return
}
let userId = user.uid
var retryCount = 0
// Function to attempt user deletion
func attemptUserDeletion() {
user.delete { error in
if let error = error {
// Retry on failure up to 3 times
if retryCount < 2 { // Retry 3 times (total attempts: 0, 1, 2)
retryCount += 1
attemptUserDeletion()
} else {
// Max retries reached
completion(.failure(error))
}
} else {
// Success
completion(.success(()))
}
}
}
// Delete user data from Firestore
FirestoreService.shared.deleteUserData(userId: userId) { result in
switch result {
case .success:
// Once Firestore data is deleted, attempt to delete user
attemptUserDeletion()
case .failure(let error):
completion(.failure(error))
}
}
}
func checkIfEmailRegistered(email: String, completion: @escaping (Bool) -> Void) {
let db = Firestore.firestore()
// Check if a document with the email exists in usersByEmail collection
db.collection("usersByEmail").document(email).getDocument { (document, error) in
if let error = error {
print("Error fetching document: \(error.localizedDescription)")
completion(false)
return
}
if let _ = document, document!.exists {
// Document with the email exists
completion(true)
} else {
// Document with the email does not exist
completion(false)
}
}
}
}