yangdx commited on
Commit
aba4c12
·
1 Parent(s): 1bd15bc

Refactor ProtectedRoute component to properly handle navigation in React lifecycle

Browse files
Files changed (1) hide show
  1. lightrag_webui/src/AppRouter.tsx +20 -21
lightrag_webui/src/AppRouter.tsx CHANGED
@@ -67,32 +67,31 @@ const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
67
  }
68
  }, [isAuthenticated])
69
 
70
- // Show nothing while checking auth status
71
- if (isChecking) {
72
- return null
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  }
74
 
75
- // After checking, if still not authenticated
76
  if (!isAuthenticated) {
77
- // Get current path and check if it's a direct access
78
- const currentPath = window.location.hash.slice(1); // Remove the '#' from hash
79
- const isLoginPage = currentPath === '/login';
80
-
81
- // Skip redirect if already on login page
82
- if (isLoginPage) {
83
- return null;
84
- }
85
-
86
- // For non-login pages, handle state reset and navigation
87
- if (!isLoginPage) {
88
- // Use navigation service for redirection
89
- console.log('Not authenticated, redirecting to login');
90
- navigationService.navigateToLogin();
91
- return null;
92
- }
93
  }
94
 
95
- return <>{children}</>
96
  }
97
 
98
  const AppContent = () => {
 
67
  }
68
  }, [isAuthenticated])
69
 
70
+ // Handle navigation when authentication status changes
71
+ useEffect(() => {
72
+ if (!isChecking && !isAuthenticated) {
73
+ const currentPath = window.location.hash.slice(1); // Remove the '#' from hash
74
+ const isLoginPage = currentPath === '/login';
75
+
76
+ if (!isLoginPage) {
77
+ // Use navigation service for redirection
78
+ console.log('Not authenticated, redirecting to login');
79
+ navigationService.navigateToLogin();
80
+ }
81
+ }
82
+ }, [isChecking, isAuthenticated]);
83
+
84
+ // Show nothing while checking auth status or when not authenticated on login page
85
+ if (isChecking || (!isAuthenticated && window.location.hash.slice(1) === '/login')) {
86
+ return null;
87
  }
88
 
89
+ // Show children only when authenticated
90
  if (!isAuthenticated) {
91
+ return null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  }
93
 
94
+ return <>{children}</>;
95
  }
96
 
97
  const AppContent = () => {