diff --git a/lightrag_webui/src/features/LoginPage.tsx b/lightrag_webui/src/features/LoginPage.tsx index 485ed8ae..20e11325 100644 --- a/lightrag_webui/src/features/LoginPage.tsx +++ b/lightrag_webui/src/features/LoginPage.tsx @@ -4,6 +4,7 @@ import { useAuthStore } from '@/stores/state' import { loginToServer, getAuthStatus } from '@/api/lightrag' import { toast } from 'sonner' import { useTranslation } from 'react-i18next' +import { navigationService } from '@/services/navigation' import { Card, CardContent, CardHeader } from '@/components/ui/Card' import Input from '@/components/ui/Input' @@ -20,6 +21,11 @@ const LoginPage = () => { const [password, setPassword] = useState('') const [checkingAuth, setCheckingAuth] = useState(true) + // Reset application state on first mount + useEffect(() => { + navigationService.resetAllApplicationState(); + }, []); // Empty dependency array means this runs only once on mount + // Check if authentication is configured useEffect(() => { let isMounted = true; // Flag to prevent state updates after unmount diff --git a/lightrag_webui/src/services/navigation.ts b/lightrag_webui/src/services/navigation.ts index 88f286ee..9da1e89d 100644 --- a/lightrag_webui/src/services/navigation.ts +++ b/lightrag_webui/src/services/navigation.ts @@ -1,4 +1,7 @@ import { NavigateFunction } from 'react-router-dom'; +import { useAuthStore, useBackendState } from '@/stores/state'; +import { useGraphStore } from '@/stores/graph'; +import { useSettingsStore } from '@/stores/settings'; class NavigationService { private navigate: NavigateFunction | null = null; @@ -7,7 +10,42 @@ class NavigationService { this.navigate = navigate; } + /** + * Reset all application state to ensure a clean environment. + * This function should be called when: + * 1. User logs out + * 2. Authentication token expires + * 3. Direct access to login page + */ + resetAllApplicationState() { + console.log('Resetting all application state...'); + + // Clear authentication state + localStorage.removeItem('LIGHTRAG-API-TOKEN'); + sessionStorage.clear(); + useAuthStore.getState().logout(); + + // Reset graph state + const graphStore = useGraphStore.getState(); + graphStore.reset(); + graphStore.setGraphDataFetchAttempted(false); + graphStore.setLabelsFetchAttempted(false); + + // Reset backend state + useBackendState.getState().clear(); + + // Reset retrieval history while preserving other user preferences + useSettingsStore.getState().setRetrievalHistory([]); + } + + /** + * Navigate to login page after resetting application state + * to ensure a clean environment for the next session + */ navigateToLogin() { + // Reset state before navigation + this.resetAllApplicationState(); + if (this.navigate) { this.navigate('/login'); }