yangdx commited on
Commit
7e2344c
·
1 Parent(s): c586d56

Keep chat history when login with same user

Browse files
lightrag_webui/src/features/LoginPage.tsx CHANGED
@@ -1,6 +1,7 @@
1
  import { useState, useEffect, useRef } from 'react'
2
  import { useNavigate } from 'react-router-dom'
3
  import { useAuthStore } from '@/stores/state'
 
4
  import { loginToServer, getAuthStatus } from '@/api/lightrag'
5
  import { toast } from 'sonner'
6
  import { useTranslation } from 'react-i18next'
@@ -94,6 +95,24 @@ const LoginPage = () => {
94
  setLoading(true)
95
  const response = await loginToServer(username, password)
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  // Check authentication mode
98
  const isGuestMode = response.auth_mode === 'disabled'
99
  login(response.access_token, isGuestMode, response.core_version, response.api_version, response.webui_title || null, response.webui_description || null)
 
1
  import { useState, useEffect, useRef } from 'react'
2
  import { useNavigate } from 'react-router-dom'
3
  import { useAuthStore } from '@/stores/state'
4
+ import { useSettingsStore } from '@/stores/settings'
5
  import { loginToServer, getAuthStatus } from '@/api/lightrag'
6
  import { toast } from 'sonner'
7
  import { useTranslation } from 'react-i18next'
 
95
  setLoading(true)
96
  const response = await loginToServer(username, password)
97
 
98
+ // Get previous username from localStorage
99
+ const previousUsername = localStorage.getItem('LIGHTRAG-PREVIOUS-USER')
100
+
101
+ // Check if it's the same user logging in again
102
+ const isSameUser = previousUsername === username
103
+
104
+ // If it's not the same user, clear chat history
105
+ if (isSameUser) {
106
+ console.log('Same user logging in, preserving chat history')
107
+ } else {
108
+ console.log('Different user logging in, clearing chat history')
109
+ // Directly clear chat history instead of setting a flag
110
+ useSettingsStore.getState().setRetrievalHistory([])
111
+ }
112
+
113
+ // Update previous username
114
+ localStorage.setItem('LIGHTRAG-PREVIOUS-USER', username)
115
+
116
  // Check authentication mode
117
  const isGuestMode = response.auth_mode === 'disabled'
118
  login(response.access_token, isGuestMode, response.core_version, response.api_version, response.webui_title || null, response.webui_description || null)
lightrag_webui/src/services/navigation.ts CHANGED
@@ -16,8 +16,10 @@ class NavigationService {
16
  * 1. User logs out
17
  * 2. Authentication token expires
18
  * 3. Direct access to login page
 
 
19
  */
20
- resetAllApplicationState() {
21
  console.log('Resetting all application state...');
22
 
23
  // Reset graph state
@@ -32,8 +34,10 @@ class NavigationService {
32
  // Reset backend state
33
  useBackendState.getState().clear();
34
 
35
- // Reset retrieval history message while preserving other user preferences
36
- useSettingsStore.getState().setRetrievalHistory([]);
 
 
37
 
38
  // Clear authentication state
39
  sessionStorage.clear();
@@ -45,21 +49,8 @@ class NavigationService {
45
  }
46
  }
47
 
48
- /**
49
- * Handle direct access to login page
50
- * @returns true if it's a direct access, false if navigated from another page
51
- */
52
- handleDirectLoginAccess() {
53
- const isDirectAccess = !document.referrer;
54
- if (isDirectAccess) {
55
- this.resetAllApplicationState();
56
- }
57
- return isDirectAccess;
58
- }
59
-
60
  /**
61
  * Navigate to login page and reset application state
62
- * @param skipReset whether to skip state reset (used for direct access scenario where reset is already handled)
63
  */
64
  navigateToLogin() {
65
  if (!this.navigate) {
@@ -67,7 +58,15 @@ class NavigationService {
67
  return;
68
  }
69
 
70
- this.resetAllApplicationState();
 
 
 
 
 
 
 
 
71
  useAuthStore.getState().logout();
72
 
73
  this.navigate('/login');
 
16
  * 1. User logs out
17
  * 2. Authentication token expires
18
  * 3. Direct access to login page
19
+ *
20
+ * @param preserveHistory If true, chat history will be preserved. Default is false.
21
  */
22
+ resetAllApplicationState(preserveHistory = false) {
23
  console.log('Resetting all application state...');
24
 
25
  // Reset graph state
 
34
  // Reset backend state
35
  useBackendState.getState().clear();
36
 
37
+ // Reset retrieval history message only if preserveHistory is false
38
+ if (!preserveHistory) {
39
+ useSettingsStore.getState().setRetrievalHistory([]);
40
+ }
41
 
42
  // Clear authentication state
43
  sessionStorage.clear();
 
49
  }
50
  }
51
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  /**
53
  * Navigate to login page and reset application state
 
54
  */
55
  navigateToLogin() {
56
  if (!this.navigate) {
 
58
  return;
59
  }
60
 
61
+ // Store current username before logout for comparison during next login
62
+ const currentUsername = useAuthStore.getState().username;
63
+ if (currentUsername) {
64
+ localStorage.setItem('LIGHTRAG-PREVIOUS-USER', currentUsername);
65
+ }
66
+
67
+ // Reset application state but preserve history
68
+ // History will be cleared on next login if the user changes
69
+ this.resetAllApplicationState(true);
70
  useAuthStore.getState().logout();
71
 
72
  this.navigate('/login');