Fix guest login expiration problem

This commit is contained in:
yangdx
2025-04-05 05:23:19 +08:00
parent ef70a55bed
commit 4fe66e7364

View File

@@ -22,6 +22,7 @@ function App() {
const enableHealthCheck = useSettingsStore.use.enableHealthCheck() const enableHealthCheck = useSettingsStore.use.enableHealthCheck()
const currentTab = useSettingsStore.use.currentTab() const currentTab = useSettingsStore.use.currentTab()
const [apiKeyAlertOpen, setApiKeyAlertOpen] = useState(false) const [apiKeyAlertOpen, setApiKeyAlertOpen] = useState(false)
const [initializing, setInitializing] = useState(true) // Add initializing state
const versionCheckRef = useRef(false); // Prevent duplicate calls in Vite dev mode const versionCheckRef = useRef(false); // Prevent duplicate calls in Vite dev mode
const handleApiKeyAlertOpenChange = useCallback((open: boolean) => { const handleApiKeyAlertOpenChange = useCallback((open: boolean) => {
@@ -55,17 +56,31 @@ function App() {
// Check if version info was already obtained in login page // Check if version info was already obtained in login page
const versionCheckedFromLogin = sessionStorage.getItem('VERSION_CHECKED_FROM_LOGIN') === 'true'; const versionCheckedFromLogin = sessionStorage.getItem('VERSION_CHECKED_FROM_LOGIN') === 'true';
if (versionCheckedFromLogin) return; if (versionCheckedFromLogin) {
setInitializing(false); // Skip initialization if already checked
// Get version info return;
const token = localStorage.getItem('LIGHTRAG-API-TOKEN'); }
if (!token) return;
try { try {
setInitializing(true); // Start initialization
// Get version info
const token = localStorage.getItem('LIGHTRAG-API-TOKEN');
const status = await getAuthStatus(); const status = await getAuthStatus();
if (status.core_version || status.api_version || status.webui_title || status.webui_description) {
// If auth is not configured and a new token is returned, use the new token
if (!status.auth_configured && status.access_token) {
useAuthStore.getState().login(
status.access_token, // Use the new token
true, // Guest mode
status.core_version,
status.api_version,
status.webui_title || null,
status.webui_description || null
);
} else if (token && (status.core_version || status.api_version || status.webui_title || status.webui_description)) {
// Otherwise use the old token (if it exists)
const isGuestMode = status.auth_mode === 'disabled' || useAuthStore.getState().isGuestMode; const isGuestMode = status.auth_mode === 'disabled' || useAuthStore.getState().isGuestMode;
// Update version info and webui title while maintaining login state
useAuthStore.getState().login( useAuthStore.getState().login(
token, token,
isGuestMode, isGuestMode,
@@ -74,12 +89,15 @@ function App() {
status.webui_title || null, status.webui_title || null,
status.webui_description || null status.webui_description || null
); );
// Set flag to indicate version info has been checked
sessionStorage.setItem('VERSION_CHECKED_FROM_LOGIN', 'true');
} }
// Set flag to indicate version info has been checked
sessionStorage.setItem('VERSION_CHECKED_FROM_LOGIN', 'true');
} catch (error) { } catch (error) {
console.error('Failed to get version info:', error); console.error('Failed to get version info:', error);
} finally {
// Ensure initializing is set to false even if there's an error
setInitializing(false);
} }
}; };
@@ -103,31 +121,42 @@ function App() {
return ( return (
<ThemeProvider> <ThemeProvider>
<TabVisibilityProvider> <TabVisibilityProvider>
<main className="flex h-screen w-screen overflow-hidden"> {initializing ? (
<Tabs // Loading state while initializing
defaultValue={currentTab} <div className="flex h-screen w-screen items-center justify-center">
className="!m-0 flex grow flex-col !p-0 overflow-hidden" <div className="text-center">
onValueChange={handleTabChange} <div className="mb-2 h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent"></div>
> <p>Initializing...</p>
<SiteHeader />
<div className="relative grow">
<TabsContent value="documents" className="absolute top-0 right-0 bottom-0 left-0 overflow-auto">
<DocumentManager />
</TabsContent>
<TabsContent value="knowledge-graph" className="absolute top-0 right-0 bottom-0 left-0 overflow-hidden">
<GraphViewer />
</TabsContent>
<TabsContent value="retrieval" className="absolute top-0 right-0 bottom-0 left-0 overflow-hidden">
<RetrievalTesting />
</TabsContent>
<TabsContent value="api" className="absolute top-0 right-0 bottom-0 left-0 overflow-hidden">
<ApiSite />
</TabsContent>
</div> </div>
</Tabs> </div>
{enableHealthCheck && <StatusIndicator />} ) : (
<ApiKeyAlert open={apiKeyAlertOpen} onOpenChange={handleApiKeyAlertOpenChange} /> // Main content after initialization
</main> <main className="flex h-screen w-screen overflow-hidden">
<Tabs
defaultValue={currentTab}
className="!m-0 flex grow flex-col !p-0 overflow-hidden"
onValueChange={handleTabChange}
>
<SiteHeader />
<div className="relative grow">
<TabsContent value="documents" className="absolute top-0 right-0 bottom-0 left-0 overflow-auto">
<DocumentManager />
</TabsContent>
<TabsContent value="knowledge-graph" className="absolute top-0 right-0 bottom-0 left-0 overflow-hidden">
<GraphViewer />
</TabsContent>
<TabsContent value="retrieval" className="absolute top-0 right-0 bottom-0 left-0 overflow-hidden">
<RetrievalTesting />
</TabsContent>
<TabsContent value="api" className="absolute top-0 right-0 bottom-0 left-0 overflow-hidden">
<ApiSite />
</TabsContent>
</div>
</Tabs>
{enableHealthCheck && <StatusIndicator />}
<ApiKeyAlert open={apiKeyAlertOpen} onOpenChange={handleApiKeyAlertOpenChange} />
</main>
)}
</TabVisibilityProvider> </TabVisibilityProvider>
</ThemeProvider> </ThemeProvider>
) )