401 global intercept to popup login form

Specifically I don’t want the boring ‘open app’; ‘login’; ‘show authenticated flow’ approach. You should be able to get pretty deep into the application before it prompts you to auth.

I’ve got some pretty horrible code to do this per ‘Screen’ now:

@Composable
fun Screen1(viewModel: MySpecificViewModel,
            signInUpViewModel: SignInUpViewModel,
            uiState: MySpecificState) {
    val authUiState by signInUpViewModel.uiState.collectAsState()
    val globalGlobalGlobalStateLocal by globalGlobalState.collectAsState()

    if (globalGlobalGlobalStateLocal.token != null) {
        viewModel.postStuff(Stuff())
    } else {
        globalMutableStateFlow.update { it.copy(showAuth = true) }
    }
}
@Composable
fun Screen0(
  viewModel: MySpecificViewModel = viewModel {  MySpecificViewModel() },
  signInUpViewModel: SignInUpViewModel = viewModel { SignInUpViewModel() }) {
    val globalGlobalGlobalStateLocal by globalGlobalState.collectAsState()
    val uiState by viewModel.uiState.collectAsState()

    if (globalGlobalGlobalStateLocal.showAuth) {
        val onDismissRequest = {
            globalMutableStateFlow.update { it.copy(showAuth = false) }
        }
        Dialog(
            onDismissRequest = onDismissRequest,
            properties = DialogProperties(
                dismissOnBackPress = false,
                dismissOnClickOutside = false
            )
        ) {
            AuthScreen(onDismissRequest = onDismissRequest)
        }
    } else {
        Screen0(viewModel = viewModel,
                signInUpViewModel = signInUpViewModel,
                uiState = uiState)
    }
}

I’m new to Kotlin Multiplatform and Compose broadly, but I’m thinking maybe a global error handling for ktor and figuring out how to use decompose [which I have integrated but don’t really know how to work with] to initiate an auth screen redirect from ktor error handling (I’m assuming some sort of event emission; so that the ktor code never needs to become Composable). Plenty of room for things to get confusing here as more auth providers are added, esp. wrt. ones which redirect back to the app (because then you state needs to be pushed around so that the initial auth-needed action is completed once auth is successful).

Any tips or sample projects would be appreciated.

Maybe this is the wrong question, but why are you using Compose with KTOR? Generally, you would use these two libraries for separate applications; make a GUI with Compose that makes web requests to a server, and the server can be written using KTOR. Are you making a desktop application? If so, you don’t need KTOR.

1 Like