Has anyone handled navigating to a login route whe...
# compose
j
Has anyone handled navigating to a login route when the user isn’t authenticated using
NavHost
in a Compose Multiplatform app? My concern is the Home screen (startDestination) may be visible until the preferences are read (using DataStore) and it’s determined the app should navigate to the Login route. In Android, this we straightforward to achieve by holding the splash screen until we’ve loaded initial state. I’m struggling to find an effective way of achieving this that works for both Android and iOS. Not sure if this is the best channel for this question. If not, please point out the best one for this question.
s
The same way you'll hold the splash screen on Android, you can hide the entire UI of your start destination. Put this logic inside the screen of your start destination itself before it decides to show the content or not
j
So my start destination would essentially have some conditional Composable that fills the entire screen? Wouldn't I potentially get false positives if my start destination is removed from the back stack and navigated to again? Such as after displaying my login destination? Once I navigate back to my start it would initialize the view model and briefly display the splash UI.
s
Your source of truth of if you're logged in or not should optimally give you a quick result. And while you're waiting for the response and you don't know what to show you could show nothing, an empty screen. If you navigate there while logged in it should optimally immediately realize that you're logged in and it doesn't have to show the "splash screen" UI
j
I'm not sure how fast DataStore loads in a worse case scenario. I assume it's fast but I doubt it's fast enough to not cause UI flickering.
Do you have a multiplatform project that uses the solution you mentioned above?
s
No I don't. But yeah I mean if you have to wait for it you need to decide what will show while waiting. After the app opening and initializing you could also store the state of if you're logged in or not at a place where fetching it is instant, so navigating back to the screen won't have any flickering. It just depends on you really
j
I think I could achieve that using some Authentication manager... Another solution I was considering was using a shared view model between Android and iOS and manually implementing a solution similar to Android where we hold the splash UI separate from the Compose UI... On iOS this requires a bit more work because I'd have to recreate the Splashboard.swift in SwiftUi and hold it programmatically then display the MainViewController once my viewmodel signals.