Keep chat history when login with same user

This commit is contained in:
yangdx
2025-04-28 02:14:50 +08:00
parent 18040aa95c
commit d7552a2a87
2 changed files with 35 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
import { useState, useEffect, useRef } from 'react'
import { useNavigate } from 'react-router-dom'
import { useAuthStore } from '@/stores/state'
import { useSettingsStore } from '@/stores/settings'
import { loginToServer, getAuthStatus } from '@/api/lightrag'
import { toast } from 'sonner'
import { useTranslation } from 'react-i18next'
@@ -94,6 +95,24 @@ const LoginPage = () => {
setLoading(true)
const response = await loginToServer(username, password)
// Get previous username from localStorage
const previousUsername = localStorage.getItem('LIGHTRAG-PREVIOUS-USER')
// Check if it's the same user logging in again
const isSameUser = previousUsername === username
// If it's not the same user, clear chat history
if (isSameUser) {
console.log('Same user logging in, preserving chat history')
} else {
console.log('Different user logging in, clearing chat history')
// Directly clear chat history instead of setting a flag
useSettingsStore.getState().setRetrievalHistory([])
}
// Update previous username
localStorage.setItem('LIGHTRAG-PREVIOUS-USER', username)
// Check authentication mode
const isGuestMode = response.auth_mode === 'disabled'
login(response.access_token, isGuestMode, response.core_version, response.api_version, response.webui_title || null, response.webui_description || null)

View File

@@ -16,8 +16,10 @@ class NavigationService {
* 1. User logs out
* 2. Authentication token expires
* 3. Direct access to login page
*
* @param preserveHistory If true, chat history will be preserved. Default is false.
*/
resetAllApplicationState() {
resetAllApplicationState(preserveHistory = false) {
console.log('Resetting all application state...');
// Reset graph state
@@ -32,8 +34,10 @@ class NavigationService {
// Reset backend state
useBackendState.getState().clear();
// Reset retrieval history message while preserving other user preferences
useSettingsStore.getState().setRetrievalHistory([]);
// Reset retrieval history message only if preserveHistory is false
if (!preserveHistory) {
useSettingsStore.getState().setRetrievalHistory([]);
}
// Clear authentication state
sessionStorage.clear();
@@ -45,21 +49,8 @@ class NavigationService {
}
}
/**
* Handle direct access to login page
* @returns true if it's a direct access, false if navigated from another page
*/
handleDirectLoginAccess() {
const isDirectAccess = !document.referrer;
if (isDirectAccess) {
this.resetAllApplicationState();
}
return isDirectAccess;
}
/**
* Navigate to login page and reset application state
* @param skipReset whether to skip state reset (used for direct access scenario where reset is already handled)
*/
navigateToLogin() {
if (!this.navigate) {
@@ -67,7 +58,15 @@ class NavigationService {
return;
}
this.resetAllApplicationState();
// Store current username before logout for comparison during next login
const currentUsername = useAuthStore.getState().username;
if (currentUsername) {
localStorage.setItem('LIGHTRAG-PREVIOUS-USER', currentUsername);
}
// Reset application state but preserve history
// History will be cleared on next login if the user changes
this.resetAllApplicationState(true);
useAuthStore.getState().logout();
this.navigate('/login');