From 8ac71c57182c6ce8f5e9725e024f1a460804ec94 Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Fri, 10 Oct 2025 22:08:23 +0530 Subject: [PATCH] fix: improve server connection handling during authentication - Updated AuthStateManager to clear the active server ID upon logout, ensuring a proper return to the server connection page. - Enhanced RouterNotifier to allow users to stay on the server connection page if authenticated, improving navigation flow. - Modified AuthenticationPage to navigate back to the server connection page instead of popping the navigation stack, enhancing user experience during server setup. --- lib/core/auth/auth_state_manager.dart | 8 ++++++++ lib/core/router/app_router.dart | 5 ++++- lib/features/auth/views/authentication_page.dart | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/core/auth/auth_state_manager.dart b/lib/core/auth/auth_state_manager.dart index 7feb4a9..e83b41e 100644 --- a/lib/core/auth/auth_state_manager.dart +++ b/lib/core/auth/auth_state_manager.dart @@ -562,6 +562,10 @@ class AuthStateManager extends _$AuthStateManager { await storage.clearAuthData(); _updateApiServiceToken(null); + // Clear active server to force return to server connection page + await storage.setActiveServerId(null); + ref.invalidate(activeServerProvider); + // Update state _update( (current) => current.copyWith( @@ -577,6 +581,10 @@ class AuthStateManager extends _$AuthStateManager { } catch (e) { DebugLogger.error('logout-failed', scope: 'auth/state', error: e); // Even if logout fails, clear local state + final storage = ref.read(optimizedStorageServiceProvider); + await storage.setActiveServerId(null); + ref.invalidate(activeServerProvider); + _update( (current) => current.copyWith( status: AuthStatus.unauthenticated, diff --git a/lib/core/router/app_router.dart b/lib/core/router/app_router.dart index f59446a..bff9e2c 100644 --- a/lib/core/router/app_router.dart +++ b/lib/core/router/app_router.dart @@ -87,10 +87,13 @@ class RouterNotifier extends ChangeNotifier { final authState = ref.read(authNavigationStateProvider); final connectivityService = ref.read(connectivityServiceProvider); + // Allow staying on server connection page if (location == Routes.serverConnection) { + // If authenticated but on server connection page, go to chat + // Otherwise stay on server connection page (for back navigation) return authState == AuthNavigationState.authenticated ? Routes.chat - : Routes.authentication; + : null; } // Check connectivity status to determine if we should show connection issue diff --git a/lib/features/auth/views/authentication_page.dart b/lib/features/auth/views/authentication_page.dart index 37bc947..d2d926c 100644 --- a/lib/features/auth/views/authentication_page.dart +++ b/lib/features/auth/views/authentication_page.dart @@ -205,7 +205,7 @@ class _AuthenticationPageState extends ConsumerState { children: [ ConduitIconButton( icon: Platform.isIOS ? CupertinoIcons.back : Icons.arrow_back, - onPressed: () => Navigator.pop(context), + onPressed: () => context.go(Routes.serverConnection), tooltip: AppLocalizations.of(context)!.backToServerSetup, ), const Spacer(),