30
30
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
31
32
32
import Foundation
33
+ import Firebase
33
34
import FirebaseAuth
34
35
import ScribbleFoundation
35
36
@@ -46,17 +47,23 @@ class SCIDAuthService: Authenticatable {
46
47
}
47
48
48
49
/// Attempts to log in with the provided email and password asynchronously.
50
+ ///
49
51
/// - Parameters:
50
52
/// - email: The email for login.
51
53
/// - password: The password for login.
52
- ///
53
- func logIn( withEmail email: String , password: String ) async throws {
54
+ func logIn(
55
+ withEmail email: String ,
56
+ password: String
57
+ ) async throws {
54
58
do {
55
- let result = try await Auth . auth ( ) . signIn ( withEmail: email, password: password)
59
+ let result = try await Auth . auth ( ) . signIn ( withEmail: email,
60
+ password: password)
56
61
self . userSession = result. user
57
62
try await loadUserData ( )
58
63
} catch {
59
- ScribbleLabApp ( ) . logger. error ( " Failed to log in with error \( error. localizedDescription) " )
64
+ ScribbleLabApp ( ) . logger. error (
65
+ " Failed to log in with error \( error. localizedDescription) - AUT-E101 "
66
+ )
60
67
}
61
68
}
62
69
@@ -72,34 +79,123 @@ class SCIDAuthService: Authenticatable {
72
79
/// - username: The username for the new user account.
73
80
///
74
81
/// - Throws: An error if user registration fails.
75
- ///
76
- func createUser( email: String , password: String , username: String ) async throws {
82
+ func createUser(
83
+ email: String ,
84
+ password: String ,
85
+ username: String
86
+ ) async throws {
77
87
do {
78
- let result = try await Auth . auth ( ) . createUser ( withEmail: email, password: password)
88
+ let result = try await Auth . auth ( ) . createUser ( withEmail: email,
89
+ password: password)
79
90
self . userSession = result. user
80
- await uploadUserData ( uid: result. user. uid, username: username, email: email)
91
+ await uploadUserData (
92
+ uid: result. user. uid,
93
+ username: username,
94
+ email: email
95
+ )
81
96
} catch {
82
- ScribbleLabApp ( ) . logger. error ( " Failed to create user with error \( error. localizedDescription) " )
97
+ ScribbleLabApp ( ) . logger. error (
98
+ " Failed to create user with error \( error. localizedDescription) - AUT-E102 "
99
+ )
83
100
}
84
101
}
85
102
103
+ /// Loads user data for the currently authenticated user.
104
+ ///
105
+ /// This function asynchronously loads user data for the currently authenticated user.
106
+ /// It retrieves user session information and fetches the corresponding user data based on the session UID.
107
+ /// The loaded user information includes details like username, email, etc.
108
+ ///
109
+ /// - Throws: An error if there's an issue while loading user data.
86
110
func loadUserData( ) async throws {
87
111
self . userSession = Auth . auth ( ) . currentUser
88
112
guard let currentUid = userSession? . uid else { return }
89
- // self.currentUser = try await
113
+ self . currentUser = try await SCIDUserService . fetchUser ( withUid : currentUid )
90
114
}
91
115
116
+ /// Signs the current user out of the application.
117
+ ///
118
+ /// This function signs out the current user session from the application.
119
+ /// It revokes the authentication session and clears any user-related data in the app.
120
+ ///
121
+ /// The function sets the user session and current user information to `nil` after sign-out.
122
+ ///
123
+ /// - Note: Any unsaved data or changes might be lost after calling this function.
92
124
nonisolated func signOut( ) {
93
-
125
+ Task {
126
+ do {
127
+ try Auth . auth ( ) . signOut ( )
128
+
129
+ await MainActor . run {
130
+ self . userSession = nil
131
+ self . currentUser = nil
132
+ }
133
+ } catch {
134
+ await ScribbleLabApp ( ) . logger. error (
135
+ " Failed to sign out due to error \( error. localizedDescription) - AUT-E103 "
136
+ )
137
+ }
138
+ }
94
139
}
95
140
96
- static func resetPassword( email: String , resetCompletion: @escaping ( Result < Bool , any Error > ) -> Void ) {
97
-
141
+ /// Sends a password reset request for the provided email.
142
+ ///
143
+ /// This function initiates a password reset request for the given email address.
144
+ ///
145
+ /// - Parameters:
146
+ /// - email: The email address for which the password reset is requested.
147
+ /// - resetCompletion: A closure indicating the result of the password reset request.
148
+ ///
149
+ /// - Note: The result is delivered asynchronously via the provided closure.
150
+ static func resetPassword(
151
+ email: String ,
152
+ resetCompletion: @escaping ( Result < Bool , any Error >
153
+ ) -> Void ) {
154
+ Auth . auth ( ) . sendPasswordReset ( withEmail: email) { ( error) in
155
+ if let error = error {
156
+ resetCompletion ( . failure( error) )
157
+ } else {
158
+ resetCompletion ( . success( true ) )
159
+ }
160
+ }
98
161
}
99
162
100
- func uploadUserData( uid: String , username: String , email: String ) async {
163
+ /// Uploads user data to Firestore.
164
+ ///
165
+ /// This private function creates a user object using the provided details, sets it as the current user,
166
+ /// encodes it, and then uploads it to Firestore.
167
+ ///
168
+ /// - Parameters:
169
+ /// - uid: The unique identifier for the user.
170
+ /// - username: The username associated with the user.
171
+ /// - email: The email address associated with the user.
172
+ ///
173
+ /// - Note: This function sets the current user with the provided details and uploads it to Firestore.
174
+ /// It is a private function and should be called from within the `SLAuthService`.
175
+ ///
176
+ /// > WARNING
177
+ /// > This function is called internally by the `SLAuthService` and should not typically be accessed directly.
178
+ ///
179
+ func uploadUserData(
180
+ uid: String ,
181
+ username: String ,
182
+ email: String
183
+ ) async {
184
+ let user = SLUser ( id: uid, username: username, email: email)
185
+ self . currentUser = user
101
186
187
+ do {
188
+ let encodedUser = try JSONEncoder ( ) . encode ( user)
189
+ if let userData = try JSONSerialization . jsonObject (
190
+ with: encodedUser,
191
+ options: [ ]
192
+ ) as? [ String : Any ] {
193
+ try await Firestore . firestore ( ) . collection ( " users " ) . document ( user. id) . setData ( userData)
194
+ }
195
+ } catch {
196
+ ScribbleLabApp ( ) . logger. error (
197
+ " Failed to upload user data \( error. localizedDescription) - SLUSRS-E204 "
198
+ )
199
+ }
102
200
}
103
-
104
-
105
201
}
0 commit comments