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 {
_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(

View File

@@ -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)

View File

@@ -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<void> 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',
);
}