fixing issue with the login and all I think?

This commit is contained in:
talksik
2021-12-20 19:53:31 -08:00
parent 1e597c48e6
commit 8019b0bf37
3 changed files with 31 additions and 15 deletions
@@ -73,11 +73,14 @@ class FirestoreService {
}
}
func createUser(user: User) {
func createUser(user: User, completion: @escaping((_ state: ServiceState) -> ())) {
do {
let _ = try db.collection(Collection.users.rawValue).addDocument(from: user)
completion(ServiceState.success("user created"))
} catch {
print("error in creating user \(error.localizedDescription)")
print("error in updating user \(error.localizedDescription)")
completion(ServiceState.error(ServiceError(description: error.localizedDescription)))
}
}
@@ -90,7 +93,7 @@ class FirestoreService {
completion(ServiceState.error(ServiceError(description: "No user id given to firestore service")))
}
} catch {
print("error in creating user \(error.localizedDescription)")
print("error in updating user \(error.localizedDescription)")
completion(ServiceState.error(ServiceError(description: error.localizedDescription)))
}
}
@@ -79,10 +79,8 @@ struct PhoneCodeVerificationView: View {
// TODO: bug one: not navigating after creating user...two: creating user with a random Id
// firebase will now verify the credential
Auth.auth().signIn(with: credential) { (res, err) in
// done with the response here, turn off loading screen
self.isLoading.toggle()
if err != nil {
self.isLoading.toggle()
self.toastText = "⚠️ Error with Verification"
self.toastSubMessage = "Code is invalid. Please try again or re-enter your phone number in the previous page."
self.showToast.toggle()
@@ -102,6 +100,7 @@ struct PhoneCodeVerificationView: View {
// if for some reason firebase couldn't get auth details
if userId == nil || userPhoneNumber == nil {
self.isLoading.toggle()
self.toastText = "⚠️ Error with Verification"
self.toastSubMessage = "Code is invalid. Please try again or re-enter your phone number in the previous page."
self.showToast.toggle()
@@ -110,10 +109,23 @@ struct PhoneCodeVerificationView: View {
return
}
self.phoneverificationViewModel.createOrUpdateUser(userId: userId!, phoneNumber: userPhoneNumber!)
// then sending to next page
self.navigationStack.push(OnboardingTrioView())
self.phoneverificationViewModel.createOrUpdateUser(userId: userId!, phoneNumber: userPhoneNumber!) {res in
switch res {
case .success(let msg):
// then sending to next page
self.navigationStack.push(OnboardingTrioView())
return
case .error(let error):
self.isLoading.toggle()
self.toastText = "⚠️ Error with Verification"
self.toastSubMessage = "Code is invalid. Please try again or re-enter your phone number in the previous page."
self.showToast.toggle()
print((err?.localizedDescription)!)
return
default: break
}
}
}
} label: {
@@ -20,19 +20,20 @@ final class PhoneVerificationViewModel : ObservableObject {
}
public func createOrUpdateUser(userId: String, phoneNumber: String) {
public func createOrUpdateUser(userId: String, phoneNumber: String, completion: @escaping((_ res: ServiceState?) -> ())) {
// TODO: set all in a transaction or batch write
// get user - 1 result set cost
self.firestoreService.getUser(userId: userId) {[weak self] resultingUser in
print("user that was received from get: \(resultingUser?.id)")
if resultingUser == nil {// if empty, create user - 1 result set cost..prolly higher cost
print("creating new user with id: \(userId)")
let newUser = User(id: userId, phoneNumber: phoneNumber)
print("verify the id stayed the same: \(newUser.id)")
self?.firestoreService.createUser(user: newUser)
self?.firestoreService.createUser(user: newUser) { res in
completion(res)
}
} else { // if not, change last logged in value
var user = resultingUser
@@ -40,7 +41,7 @@ final class PhoneVerificationViewModel : ObservableObject {
user!.lastLoggedInTimestamp = nil
self?.firestoreService.updateUser(user: user!) {res in
print(res)
completion(res)
}
}
}