Skip to content

Commit 56769be

Browse files
committed
(chore): Fix Swift-Lint issues and complete SCIDAuthService
1 parent 93ac88f commit 56769be

File tree

2 files changed

+115
-18
lines changed

2 files changed

+115
-18
lines changed

ScribbleLab/Misc/User.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import FirebaseAuth
3535
/// A model that represents a user in the ScribbleLab application.
3636
///
3737
/// `SLUser` holds essential user information, such as a unique identifier, username, email, and profile image URL.
38-
/// Additionally, it offers a computed property to determine if the `SLUser` instance represents the currently logged-in user.
38+
/// Additionally, it offers a computed property to determine if the `SLUser` instance represents the currently
39+
/// logged-in user.
3940
///
4041
/// - Note: `SLUser` conforms to `Identifiable`, `Hashable`, and `Codable`, making it suitable for lists, comparison,
4142
/// and serialization.
@@ -54,7 +55,7 @@ struct SLUser: Identifiable, Hashable, Codable {
5455
/// An optional URL string pointing to the user’s profile image.
5556
///
5657
/// This image is displayed as the user’s avatar within the application. If `nil`, a default image can be used.
57-
var profileImageUrl: String? = nil
58+
var profileImageUrl: String?
5859

5960
/// The user's full name.
6061
///

ScribbleLab/Services/SCIDAuthentication/SCIDAuthService.swift

Lines changed: 112 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131

3232
import Foundation
33+
import Firebase
3334
import FirebaseAuth
3435
import ScribbleFoundation
3536

@@ -46,17 +47,23 @@ class SCIDAuthService: Authenticatable {
4647
}
4748

4849
/// Attempts to log in with the provided email and password asynchronously.
50+
///
4951
/// - Parameters:
5052
/// - email: The email for login.
5153
/// - 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 {
5458
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)
5661
self.userSession = result.user
5762
try await loadUserData()
5863
} 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+
)
6067
}
6168
}
6269

@@ -72,34 +79,123 @@ class SCIDAuthService: Authenticatable {
7279
/// - username: The username for the new user account.
7380
///
7481
/// - 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 {
7787
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)
7990
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+
)
8196
} 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+
)
83100
}
84101
}
85102

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.
86110
func loadUserData() async throws {
87111
self.userSession = Auth.auth().currentUser
88112
guard let currentUid = userSession?.uid else { return }
89-
// self.currentUser = try await
113+
self.currentUser = try await SCIDUserService.fetchUser(withUid: currentUid)
90114
}
91115

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.
92124
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+
}
94139
}
95140

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+
}
98161
}
99162

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
101186

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+
}
102200
}
103-
104-
105201
}

0 commit comments

Comments
 (0)