From 4344d04ff9a0659aa250140e1feac8f6f87a6c82 Mon Sep 17 00:00:00 2001 From: StefanMoCoAt Date: Mon, 16 Mar 2026 23:35:49 +0100 Subject: [PATCH] Add authentication check to secure ping action and propagate auth state in PingScreen --- .../at/mocode/ping/feature/presentation/PingScreen.kt | 10 ++++++---- .../mocode/ping/feature/presentation/PingViewModel.kt | 6 ++++++ .../src/commonMain/kotlin/MainApp.kt | 4 +++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/frontend/features/ping-feature/src/commonMain/kotlin/at/mocode/ping/feature/presentation/PingScreen.kt b/frontend/features/ping-feature/src/commonMain/kotlin/at/mocode/ping/feature/presentation/PingScreen.kt index 87e626ca..57cb3cb1 100644 --- a/frontend/features/ping-feature/src/commonMain/kotlin/at/mocode/ping/feature/presentation/PingScreen.kt +++ b/frontend/features/ping-feature/src/commonMain/kotlin/at/mocode/ping/feature/presentation/PingScreen.kt @@ -22,8 +22,10 @@ import at.mocode.frontend.core.designsystem.theme.Dimens @Composable fun PingScreen( viewModel: PingViewModel, - onBack: () -> Unit = {} + onBack: () -> Unit = {}, + isAuthenticated: Boolean = false ) { + viewModel.isAuthenticated = isAuthenticated val uiState = viewModel.uiState // Wir nutzen jetzt das globale Theme (Hintergrund kommt vom Theme) @@ -51,7 +53,7 @@ fun PingScreen( .fillMaxHeight() .padding(end = Dimens.SpacingS) ) { - ActionToolbar(viewModel) + ActionToolbar(viewModel, isAuthenticated) Spacer(Modifier.height(Dimens.SpacingS)) StatusGrid(uiState) } @@ -131,7 +133,7 @@ private fun StatusBadge(text: String, color: Color) { } @Composable -private fun ActionToolbar(viewModel: PingViewModel) { +private fun ActionToolbar(viewModel: PingViewModel, isAuthenticated: Boolean) { // Wrap buttons to avoid overflow on small screens Row( modifier = Modifier.fillMaxWidth(), @@ -139,7 +141,7 @@ private fun ActionToolbar(viewModel: PingViewModel) { ) { DenseButton(text = "Simple", onClick = { viewModel.performSimplePing() }) DenseButton(text = "Enhanced", onClick = { viewModel.performEnhancedPing() }) - DenseButton(text = "Secure", onClick = { viewModel.performSecurePing() }) + DenseButton(text = "Secure", onClick = { viewModel.performSecurePing() }, enabled = isAuthenticated) DenseButton(text = "Health", onClick = { viewModel.performHealthCheck() }) DenseButton( text = "Sync", diff --git a/frontend/features/ping-feature/src/commonMain/kotlin/at/mocode/ping/feature/presentation/PingViewModel.kt b/frontend/features/ping-feature/src/commonMain/kotlin/at/mocode/ping/feature/presentation/PingViewModel.kt index c24b8547..a3d65895 100644 --- a/frontend/features/ping-feature/src/commonMain/kotlin/at/mocode/ping/feature/presentation/PingViewModel.kt +++ b/frontend/features/ping-feature/src/commonMain/kotlin/at/mocode/ping/feature/presentation/PingViewModel.kt @@ -41,6 +41,8 @@ class PingViewModel( var uiState by mutableStateOf(PingUiState()) private set + var isAuthenticated: Boolean = false + private fun addLog(source: String, message: String, isError: Boolean = false) { val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()) val timeString = "${now.hour.toString().padStart(2, '0')}:${now.minute.toString().padStart(2, '0')}:${ @@ -108,6 +110,10 @@ class PingViewModel( } fun performSecurePing() { + if (!isAuthenticated) { + addLog("SecurePing", "Not authenticated – request blocked.", isError = true) + return + } viewModelScope.launch { uiState = uiState.copy(isLoading = true, errorMessage = null) addLog("SecurePing", "Sending authenticated request...") diff --git a/frontend/shells/meldestelle-portal/src/commonMain/kotlin/MainApp.kt b/frontend/shells/meldestelle-portal/src/commonMain/kotlin/MainApp.kt index e668e92a..8056eb1a 100644 --- a/frontend/shells/meldestelle-portal/src/commonMain/kotlin/MainApp.kt +++ b/frontend/shells/meldestelle-portal/src/commonMain/kotlin/MainApp.kt @@ -36,6 +36,7 @@ fun MainApp() { // Delta-Sync blueprint: resolve the Ping feature view model via Koin. val pingViewModel: PingViewModel = koinViewModel() val loginViewModel: LoginViewModel = koinViewModel() + val authState by authTokenManager.authState.collectAsState() when (currentScreen) { is AppScreen.Landing -> LandingScreen( @@ -58,7 +59,8 @@ fun MainApp() { is AppScreen.Ping -> PingScreen( viewModel = pingViewModel, - onBack = { navigationPort.navigateToScreen(AppScreen.Home) } // Navigate back to Home + onBack = { navigationPort.navigateToScreen(AppScreen.Home) }, + isAuthenticated = authState.isAuthenticated ) is AppScreen.Profile -> AuthStatusScreen(