Skip to content

Commit

Permalink
Fix: user not stored in users collection
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Dec 6, 2024
1 parent eea4ce9 commit f7849bc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Data/Data/Repository/UserRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public class UserRepository: ObservableObject {

public func storeUser(user: AppUser) async throws -> AppUser {
if let storedUser = try await store.fetchUserBy(id: user.id) {
LogD("User already exists in Firestore: \(storedUser)")
return storedUser
} else {
_ = try await store.addUser(user: user)
LogD("User does not exist. Adding new user: \(user)")
try await store.addUser(user: user)
return user
}
}
Expand Down
9 changes: 8 additions & 1 deletion Data/Data/Store/UserStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ class UserStore: ObservableObject {

func fetchUserBy(id: String) async throws -> AppUser? {
let snapshot = try await usersCollection.document(id).getDocument(source: .server)
return try snapshot.data(as: AppUser.self)

if snapshot.exists {
LogD("UserStore: \(#function) Fetched document data: \(snapshot.data() ?? [:])")
return try snapshot.data(as: AppUser.self)
} else {
LogE("UserStore: \(#function) Document with id \(id) does not exist.")
return nil
}
}

func fetchLatestUserBy(id: String, completion: @escaping (AppUser?) -> Void) {
Expand Down
7 changes: 5 additions & 2 deletions Splito/UI/Login/EmailLogin/EmailLoginViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ public class EmailLoginViewModel: BaseViewModel, ObservableObject {

// MARK: - Error handling
private func handleFirebaseAuthErrors(_ error: Error, isPasswordReset: Bool = false) {
let errorCode = (error as NSError).code
guard let authErrorCode = FirebaseAuth.AuthErrorCode(rawValue: (error as NSError).code) else {
showAlertFor(title: "Error", message: "Something went wrong! Please try after some time.")
return
}

switch FirebaseAuth.AuthErrorCode(rawValue: errorCode) {
switch authErrorCode {
case .webContextCancelled:
showAlertFor(message: "Something went wrong! Please try after some time.")
case .tooManyRequests:
Expand Down

0 comments on commit f7849bc

Please sign in to comment.