refactor(auth): Preserve server configs during logout for seamless re-login

This commit is contained in:
cogwheel0
2025-12-07 10:05:55 +05:30
parent 13b0b6345a
commit 218d061eae
3 changed files with 22 additions and 22 deletions

View File

@@ -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<void> logout() async { Future<void> logout() async {
_update( _update(
(current) => (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); final storage = ref.read(optimizedStorageServiceProvider);
await storage.clearAuthData(); await storage.clearAuthData();
_updateApiServiceToken(null); _updateApiServiceToken(null);
// Clear active server to force return to server connection page // Keep active server ID so router redirects to sign-in page, not server
await storage.setActiveServerId(null); // connection page. Users can navigate to server settings if they need to
// change server configuration.
// Invalidate all auth-related providers to clear cached data // Invalidate tools provider to clear cached data
ref.invalidate(activeServerProvider);
ref.invalidate(serverConfigsProvider);
ref.invalidate(toolsListProvider); ref.invalidate(toolsListProvider);
// Clear auth cache manager // Clear auth cache manager
@@ -825,7 +827,7 @@ class AuthStateManager extends _$AuthStateManager {
); );
DebugLogger.auth( 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) { } catch (e, stack) {
DebugLogger.error( DebugLogger.error(
@@ -845,9 +847,7 @@ class AuthStateManager extends _$AuthStateManager {
error: clearError, error: clearError,
); );
} }
await storage.setActiveServerId(null); // Keep active server ID for redirect to sign-in page
ref.invalidate(activeServerProvider);
ref.invalidate(serverConfigsProvider);
_cacheManager.clearAuthCache(); _cacheManager.clearAuthCache();
_update( _update(

View File

@@ -145,7 +145,10 @@ class RouterNotifier extends ChangeNotifier {
return location == Routes.splash ? null : Routes.splash; return location == Routes.splash ? null : Routes.splash;
case AuthNavigationState.needsLogin: case AuthNavigationState.needsLogin:
if (location == Routes.connectionIssue) return null; 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: case AuthNavigationState.error:
final authSnapshot = ref final authSnapshot = ref
.read(authStateManagerProvider) .read(authStateManagerProvider)

View File

@@ -762,13 +762,13 @@ class OptimizedStorageService {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Batch operations // Batch operations
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
/// Clear all authentication-related data including credentials, tokens, /// Clear authentication-related data (tokens, credentials, user data).
/// server configurations, and custom headers /// Server configurations (URL, custom headers, self-signed cert settings)
/// are preserved to allow quick re-login.
Future<void> clearAuthData() async { Future<void> clearAuthData() async {
await Future.wait([ await Future.wait([
deleteAuthToken(), deleteAuthToken(),
deleteSavedCredentials(), deleteSavedCredentials(),
_preferencesBox.delete(_activeServerIdKey),
_cachesBox.delete(_localUserKey), _cachesBox.delete(_localUserKey),
_cachesBox.delete(_localUserAvatarKey), _cachesBox.delete(_localUserAvatarKey),
_cachesBox.delete(_localBackendConfigKey), _cachesBox.delete(_localBackendConfigKey),
@@ -776,19 +776,16 @@ class OptimizedStorageService {
_cachesBox.delete(_localToolsKey), _cachesBox.delete(_localToolsKey),
_cachesBox.delete(_localDefaultModelKey), _cachesBox.delete(_localDefaultModelKey),
_cachesBox.delete(_localModelsKey), _cachesBox.delete(_localModelsKey),
// Clear server configurations (which include custom headers) // Note: Server configs are NOT cleared - they persist across logouts
_secureCredentialStorage.clearAll(), // so users can quickly re-login without re-entering server details
]); ]);
_cacheManager.invalidateMatching( _cacheManager.invalidateMatching(
(key) => (key) => key.contains('auth') || key.contains('credentials'),
key.contains('auth') ||
key.contains('credentials') ||
key.contains('server'),
); );
DebugLogger.log( 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', scope: 'storage/optimized',
); );
} }