routing working i think? good enough getting things in place

This commit is contained in:
talksik
2021-12-19 19:18:46 -08:00
parent c8b383449e
commit 1cca80166f
3 changed files with 27 additions and 25 deletions
+11 -8
View File
@@ -6,6 +6,7 @@
//
import Foundation
import Firebase
import FirebaseFirestoreSwift
struct User: Identifiable, Codable {
@@ -18,14 +19,16 @@ struct User: Identifiable, Codable {
@ServerTimestamp var lastLoggedInTimestamp: Date?
@ServerTimestamp var createdTimestamp: Date?
enum CodingKeys: String, CodingKey {
case nickname
case phoneNumber
case emailAddress
case avatar
case lastLoggedInTimestamp
case createdTimestamp
}
// careful, if using codingkeys, you have to mapping every attribute thereafter
// enum CodingKeys: String, CodingKey {
// case id
// case nickname
// case phoneNumber
// case emailAddress
// case avatar
// case lastLoggedInTimestamp
// case createdTimestamp
// }
}
struct TestUser: Identifiable, Hashable {
+2 -2
View File
@@ -116,6 +116,7 @@ final class AuthSessionStore: ObservableObject, SessionStore {
func setupAuthListen() {
// monitor authentication changes using firebase
print("let's see what user looks like in authsession at at the start: \(self.user)")
self.handler = Auth.auth().addStateDidChangeListener { [weak self] res, user in
print("auth listener activated")
@@ -135,10 +136,9 @@ final class AuthSessionStore: ObservableObject, SessionStore {
print("user from firestore in auth listener: \(firestoreUser)")
if firestoreUser != nil {
self?.user = firestoreUser
print("lets see if authsessionstore user is being set: \(self?.user)")
}
}
self.sessionState = .isAuthenticated
} else {
// if we don't have a user, set our session to nil
self.user = nil
+14 -15
View File
@@ -30,25 +30,24 @@ struct RouterView: View {
@EnvironmentObject var navigationStack: NavigationStack
var body: some View {
SplashView()
.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())
// authenticated and have user's data
if self.authSessionStore.sessionState == SessionState.isAuthenticated {
if self.authSessionStore.user != nil {
if self.authSessionStore.user?.nickname == nil && self.authSessionStore.user?.avatar == nil {
OnboardingTrioView()
} else {// user is existing user/up and running user -> signs in is pushed nicely right to hub
self.navigationStack.push(InnerCircleView())
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())
OnboardingTrioView()
}
} else if self.authSessionStore.sessionState == SessionState.isLoggedOut {
// go to welcome page at which point they can go and follow the programmatic
// line to the sigin and so on and so forth
WelcomeView()
} else {
SplashView()
}
}
}