Best way to store login state jetpack compose?

Viewed 801

I have an app that uses firebase auth and before users can access the app they have to log in and complete their profile. I have to keep track of these two values (isLoggedIn and isProfileCompleted) on all screens.

I have created a isloggedin function that returns a boolean if the user is logged in. And also created a isProfileCompleted(onTrue: () -> Unit, onFalse: () -> Unit) which checks if the profile is completed.

With this I tried to implement navigation using them so when the user logs in we jump to the home screen or the complete profile screen depending on the value, the problem I have is that the screen is not recomposing so when I press the login button the isloggedin method is not called again so I only move to the home screen after pressing the login button if I close and open the app.

Should I save if the user is logged in and the profile is completed in each of the viewmodel states so the screen recomposes?

fun LoginSignUpNavigation(
    activity: Activity,
    navController: NavHostController,
    loginViewModel: LoginViewModel = hiltViewModel()
) {
    val startDestination = LoginSignUpRoutes.Login.routeName
    if (isLoggedIn()) {
        isProfileCompleted(
            onTrue = {
                goToHome(activity)
            },
            onFalse = {
                goToCompleteProfile(activity)
            }
        )
    }

    NavHost(navController = navController, startDestination = startDestination) {
        composable(route = LoginSignUpRoutes.Login.routeName) {
            LoginScreen(
                emailLoginClick = { navController.navigate(LoginSignUpRoutes.EmailLogin.routeName) },
                signUpLoginClick = { navController.navigate(LoginSignUpRoutes.SignUp.routeName) },
                viewModel = loginViewModel
            )
        }
        composable(route = LoginSignUpRoutes.EmailLogin.routeName) {
            EmailLoginScreen(loginViewModel)
        }
        composable(route = LoginSignUpRoutes.SignUp.routeName) {
            SignUpScreen(
                viewModel = loginViewModel
            )
        }
    }
}```
0 Answers
Related