0

I'm trying to create logic that will send the user to the correct view once they have entered their credentials. In the login page, when the user logs in they get a token that can be used to call an API. I store that token in KeychainSwift. I then can check to see if the field where I stored it has an entry that is not null and not empty to route the user to the main page. I'm using logic like this and this to route me to the correct view.

My content view code is as follows:

struct ContentView: View {
    @ObservedObject var userAuth = UserAuth()
    var body: some View {
        if(self.userAuth.isLoggedIn){
            return AnyView(MainPageView())
        }else{
            return AnyView(AccountPageView())
        }
    }
}

UserAuth is as follows:

import Combine
class UserAuth: ObservableObject {
//    let objectWillChange = ObservableObjectPublisher()
    @Published var isLoggedIn: Bool = false;
    init() {
        let keychain = KeychainSwift()
        let userToken = keychain.get("userToken") ?? ""
        if(!userToken.isEmpty || userToken != "" ){
            self.isLoggedIn = true
        }else{
            self.isLoggedIn = false
        }
    }
}

The overall architecture im trying to achieve is this: enter image description here

The issue is that there is no direct way to route from a view to another hence why I resorted to using logic that that displayed in ContentView. I have similar logic for login. I have a view (loginformview) that either routes you to the login form view or to the main page view. But once you login in the login form I would need to go back to the previous view (loginformview) where the check exist to see if I should return the login page or go to the main page(this time sending you to the main page). It seems very cyclical, anyone had any idea to proper tackle logic like this ?

2

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.