/
KeoFoxy
/
Uber
Обзор
Документация
Войти
/
KeoFoxy
/
Uber
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
Core/Authentication/ViewModels/AuthViewModel.swift
75 строк
2 KB
KeoFoxy
Fetching drivers refactoring using Combine
01 авг 2023, 22:54
01 авг 2023, 22:54
488ab2e
Код
Авторство
О чём код?
// // AuthViewModel.swift // Uber // // Created by Alexander Sorokin on 29.07.2023. // import Foundation import Firebase import FirebaseFirestoreSwift import Combine class AuthViewModel: ObservableObject { @Published var userSession: FirebaseAuth.User? @Published var currentUser: User? private let service = UserService.shared private var cancellable = Set<AnyCancellable>() init() { userSession = Auth.auth().currentUser fetchUser() } func signIn(withEmail email: String, password: String) { Auth.auth().signIn(withEmail: email, password: password) { result, error in if let error = error { print("DEBUG: Failed to sign in with error \(error.localizedDescription)") return } self.userSession = result?.user self.fetchUser() } } func registerUser(withEmai email: String, password: String, fullname: String) { guard let location = LocationManager.shared.userLocation else { return } Auth.auth().createUser(withEmail: email, password: password) { result, error in if let error = error { print("DEBUG: Failed to sign up with error \(error.localizedDescription)") return } guard let firebaseUser = result?.user else { return } self.userSession = firebaseUser let user = User(fullname: fullname, email: email, uid: firebaseUser.uid, coordinates: GeoPoint(latitude: location.latitude, longitude: location.longitude), accountType: .driver) self.currentUser = user guard let encodedUser = try? Firestore.Encoder().encode(user) else { return } Firestore.firestore().collection("users").document(firebaseUser.uid).setData(encodedUser) } } func signOut() { do { try Auth.auth().signOut() self.userSession = nil } catch let error { print("DEBUG: Failed to sign out with error: \(error.localizedDescription)") } } func fetchUser() { service.$user .sink { user in self.currentUser = user } .store(in: &cancellable) } }