diff --git a/nirvana-ios/ContentView.swift b/nirvana-ios/ContentView.swift index debd32d..24532f3 100644 --- a/nirvana-ios/ContentView.swift +++ b/nirvana-ios/ContentView.swift @@ -11,7 +11,7 @@ import NavigationStack struct ContentView: View { var body: some View { NavigationStackView { - RouterView() + RouterView() } } } diff --git a/nirvana-ios/Models/Messages.swift b/nirvana-ios/Models/Messages.swift index 07c37ed..8dc5d24 100644 --- a/nirvana-ios/Models/Messages.swift +++ b/nirvana-ios/Models/Messages.swift @@ -9,7 +9,7 @@ import Foundation import FirebaseFirestoreSwift struct Messages: Identifiable, Codable { - @DocumentID var id: String? = UUID().uuidString + @DocumentID var id: String? var senderId: String var receiverId: String diff --git a/nirvana-ios/Models/User.swift b/nirvana-ios/Models/User.swift index f0a4df3..92f45a9 100644 --- a/nirvana-ios/Models/User.swift +++ b/nirvana-ios/Models/User.swift @@ -9,7 +9,7 @@ import Foundation import FirebaseFirestoreSwift struct User: Identifiable, Codable { - @DocumentID var id: String? = UUID().uuidString + @DocumentID var id: String? var nickname: String? var phoneNumber: String? var emailAddress:String? diff --git a/nirvana-ios/Services/AuthSessionStore.swift b/nirvana-ios/Services/AuthSessionStore.swift index 3c68018..ee7a99a 100644 --- a/nirvana-ios/Services/AuthSessionStore.swift +++ b/nirvana-ios/Services/AuthSessionStore.swift @@ -121,17 +121,22 @@ final class AuthSessionStore: ObservableObject, SessionStore { guard let self = self else { return } + self.sessionState = user == nil ? SessionState.isLoggedOut : SessionState.isAuthenticated // get the user from the auth table in firebase auth - if let user = user { + if let uid = user?.uid { // if we have a user, create a new user model - print("auth listener: Got user: \(user.uid)") - print("auth listener: phone number: \(user.phoneNumber!)") + print("auth listener: Got user: \(uid)") + print("auth listener: phone number: \(user?.phoneNumber)") - // TODO: go to firestore and get full user document for the current authenticated user - // and have this in environment for all pages to access without having to fetch all the time - - self.getAndSetEnvironmentUserDetails(userId: user.uid) + // get user from firestore and store in environment + self.firestoreService.getUser(userId: uid) {[weak self] firestoreUser in + // if we get a user back, then set this to our authsessionstore for our views + print("user from firestore in auth listener: \(firestoreUser)") + if firestoreUser != nil { + self?.user = firestoreUser + } + } self.sessionState = .isAuthenticated } else { @@ -141,18 +146,6 @@ final class AuthSessionStore: ObservableObject, SessionStore { } } } - - private func getAndSetEnvironmentUserDetails(userId: String) { - self.firestoreService.getUser(userId: userId) {firestoreUser in - // if we get a user back, then set this to our instance to allow UI to get published with all user details - if firestoreUser != nil { - // TODO: prolly useless since we never set up a background thread for the rest of this code before - DispatchQueue.main.async { - self.user = firestoreUser - } - } - } - } } extension UIApplication { diff --git a/nirvana-ios/Views/Router/RouterView.swift b/nirvana-ios/Views/Router/RouterView.swift index a655460..590c93d 100644 --- a/nirvana-ios/Views/Router/RouterView.swift +++ b/nirvana-ios/Views/Router/RouterView.swift @@ -31,21 +31,20 @@ struct RouterView: View { var body: some View { SplashView() - .onAppear() { - print(authSessionStore) - print("figuring out where to send the user based on \(self.authSessionStore)") - if authSessionStore.sessionState == SessionState.isAuthenticated { - if let user = authSessionStore.user { - // evaluate where the user should go based on how much data he has - - // user is authenticated but has never picked a username or avatar - if user.nickname != nil && user.avatar != nil { - self.navigationStack.push(OnboardingTrioView()) - } else {// user is existing user for a year -> signs in is pushed nicely right to hub - self.navigationStack.push(InnerCircleView()) - } + .onReceive(self.authSessionStore.$user) {updatedUserFromAuth in // observing user because takes longer to get the user details than see updates to sessionstate and we need the user for the logic below + print("figuring out where to send the user based on state: \(self.authSessionStore.sessionState)") + if self.authSessionStore.sessionState == SessionState.isAuthenticated && updatedUserFromAuth != nil { + print("user stored in authsession store: \(updatedUserFromAuth)") + // evaluate where the user should go based on how much data he has + + // user is authenticated but has never picked a username or avatar + if updatedUserFromAuth?.nickname != nil && updatedUserFromAuth?.avatar != nil { + self.navigationStack.push(OnboardingTrioView()) + } else {// user is existing user/up and running user -> signs in is pushed nicely right to hub + self.navigationStack.push(InnerCircleView()) } } else { + print("sending user to welcome view") // go to welcome page at which point they can go and follow the programmatic // line to the sigin and so on and so forth self.navigationStack.push(WelcomeView())