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