From 218d061eaefa6475ff93829c3f748118cfff2eef Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Sun, 7 Dec 2025 10:05:55 +0530 Subject: [PATCH] refactor(auth): Preserve server configs during logout for seamless re-login --- lib/core/auth/auth_state_manager.dart | 22 +++++++++---------- lib/core/router/app_router.dart | 5 ++++- .../services/optimized_storage_service.dart | 17 ++++++-------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/core/auth/auth_state_manager.dart b/lib/core/auth/auth_state_manager.dart index bc85dd0..86bd28c 100644 --- a/lib/core/auth/auth_state_manager.dart +++ b/lib/core/auth/auth_state_manager.dart @@ -775,7 +775,10 @@ class AuthStateManager extends _$AuthStateManager { } } - /// Logout user and clear all data including server configs and custom headers + /// Logout user and clear auth data while preserving server configuration. + /// Server settings (URL, custom headers, self-signed cert) are kept so users + /// can quickly re-login. Users can navigate to server connection page to + /// change server settings if needed. Future logout() async { _update( (current) => @@ -797,17 +800,16 @@ class AuthStateManager extends _$AuthStateManager { } } - // Clear all local auth data (including server configs with custom headers) + // Clear auth data but preserve server configs (URL, headers, cert settings) final storage = ref.read(optimizedStorageServiceProvider); await storage.clearAuthData(); _updateApiServiceToken(null); - // Clear active server to force return to server connection page - await storage.setActiveServerId(null); + // Keep active server ID so router redirects to sign-in page, not server + // connection page. Users can navigate to server settings if they need to + // change server configuration. - // Invalidate all auth-related providers to clear cached data - ref.invalidate(activeServerProvider); - ref.invalidate(serverConfigsProvider); + // Invalidate tools provider to clear cached data ref.invalidate(toolsListProvider); // Clear auth cache manager @@ -825,7 +827,7 @@ class AuthStateManager extends _$AuthStateManager { ); DebugLogger.auth( - 'Logout complete - all data cleared including server configs and custom headers', + 'Logout complete - auth data cleared, server config preserved for quick re-login', ); } catch (e, stack) { DebugLogger.error( @@ -845,9 +847,7 @@ class AuthStateManager extends _$AuthStateManager { error: clearError, ); } - await storage.setActiveServerId(null); - ref.invalidate(activeServerProvider); - ref.invalidate(serverConfigsProvider); + // Keep active server ID for redirect to sign-in page _cacheManager.clearAuthCache(); _update( diff --git a/lib/core/router/app_router.dart b/lib/core/router/app_router.dart index 71fdd4f..1ad2d03 100644 --- a/lib/core/router/app_router.dart +++ b/lib/core/router/app_router.dart @@ -145,7 +145,10 @@ class RouterNotifier extends ChangeNotifier { return location == Routes.splash ? null : Routes.splash; case AuthNavigationState.needsLogin: if (location == Routes.connectionIssue) return null; - return null; + // Redirect to authentication page if not already on an auth route + // This handles the post-logout case where we want sign-in, not server setup + if (_isAuthLocation(location)) return null; + return Routes.authentication; case AuthNavigationState.error: final authSnapshot = ref .read(authStateManagerProvider) diff --git a/lib/core/services/optimized_storage_service.dart b/lib/core/services/optimized_storage_service.dart index 17e56a6..bc8f447 100644 --- a/lib/core/services/optimized_storage_service.dart +++ b/lib/core/services/optimized_storage_service.dart @@ -762,13 +762,13 @@ class OptimizedStorageService { // --------------------------------------------------------------------------- // Batch operations // --------------------------------------------------------------------------- - /// Clear all authentication-related data including credentials, tokens, - /// server configurations, and custom headers + /// Clear authentication-related data (tokens, credentials, user data). + /// Server configurations (URL, custom headers, self-signed cert settings) + /// are preserved to allow quick re-login. Future clearAuthData() async { await Future.wait([ deleteAuthToken(), deleteSavedCredentials(), - _preferencesBox.delete(_activeServerIdKey), _cachesBox.delete(_localUserKey), _cachesBox.delete(_localUserAvatarKey), _cachesBox.delete(_localBackendConfigKey), @@ -776,19 +776,16 @@ class OptimizedStorageService { _cachesBox.delete(_localToolsKey), _cachesBox.delete(_localDefaultModelKey), _cachesBox.delete(_localModelsKey), - // Clear server configurations (which include custom headers) - _secureCredentialStorage.clearAll(), + // Note: Server configs are NOT cleared - they persist across logouts + // so users can quickly re-login without re-entering server details ]); _cacheManager.invalidateMatching( - (key) => - key.contains('auth') || - key.contains('credentials') || - key.contains('server'), + (key) => key.contains('auth') || key.contains('credentials'), ); DebugLogger.log( - 'Auth data cleared in batch operation (including server configs and custom headers)', + 'Auth data cleared (server configs preserved for quick re-login)', scope: 'storage/optimized', ); }