Prevent login page show up when on auth is needed

This commit is contained in:
yangdx
2025-03-23 02:00:59 +08:00
parent 3609dc5b26
commit d3b2cff182
2 changed files with 5 additions and 17 deletions

View File

@@ -19,14 +19,13 @@ const AppContent = () => {
// Token validity check // Token validity check
useEffect(() => { useEffect(() => {
let isMounted = true;
const checkAuth = async () => { const checkAuth = async () => {
try { try {
const token = localStorage.getItem('LIGHTRAG-API-TOKEN') const token = localStorage.getItem('LIGHTRAG-API-TOKEN')
if (token && isAuthenticated) { if (token && isAuthenticated) {
if (isMounted) setInitializing(false); setInitializing(false);
return; return;
} }
@@ -35,21 +34,17 @@ const AppContent = () => {
} }
} catch (error) { } catch (error) {
console.error('Auth initialization error:', error) console.error('Auth initialization error:', error)
if (isMounted && !isAuthenticated) { if (!isAuthenticated) {
useAuthStore.getState().logout() useAuthStore.getState().logout()
} }
} finally { } finally {
if (isMounted) {
setInitializing(false) setInitializing(false)
} }
} }
}
checkAuth() checkAuth()
return () => { return () => {
isMounted = false;
setInitializing(false)
} }
}, [isAuthenticated]) }, [isAuthenticated])

View File

@@ -26,7 +26,6 @@ const LoginPage = () => {
// Check if authentication is configured, skip login if not // Check if authentication is configured, skip login if not
useEffect(() => { useEffect(() => {
let isMounted = true; // Flag to prevent state updates after unmount
const checkAuthConfig = async () => { const checkAuthConfig = async () => {
// Prevent duplicate calls in Vite dev mode // Prevent duplicate calls in Vite dev mode
@@ -61,17 +60,13 @@ const LoginPage = () => {
} }
// Only set checkingAuth to false if we need to show the login page // Only set checkingAuth to false if we need to show the login page
if (isMounted) {
setCheckingAuth(false); setCheckingAuth(false);
}
} catch (error) { } catch (error) {
console.error('Failed to check auth configuration:', error) console.error('Failed to check auth configuration:', error)
// Also set checkingAuth to false in case of error // Also set checkingAuth to false in case of error
if (isMounted) {
setCheckingAuth(false); setCheckingAuth(false);
} }
}
// Removed finally block as we're setting checkingAuth earlier // Removed finally block as we're setting checkingAuth earlier
} }
@@ -80,8 +75,6 @@ const LoginPage = () => {
// Cleanup function to prevent state updates after unmount // Cleanup function to prevent state updates after unmount
return () => { return () => {
isMounted = false;
setCheckingAuth(false);
} }
}, [isAuthenticated, login, navigate]) }, [isAuthenticated, login, navigate])