diff --git a/src/App.jsx b/src/App.jsx index 64e3584..a2f2728 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -21,6 +21,7 @@ import Admin from './pages/Admin'; function App() { const [user, setUser] = useState(null); const [isAdmin, setIsAdmin] = useState(false); + const [isLoading, setIsLoading] = useState(true); const fetchProfile = async () => { try { @@ -29,8 +30,14 @@ function App() { setIsAdmin(response.user.role === 'admin'); } catch (error) { console.error('Failed to fetch profile:', error); - setUser(null); - setIsAdmin(false); + // Only logout on authentication errors (401/403), not network errors + if (error.response && (error.response.status === 401 || error.response.status === 403)) { + setUser(null); + setIsAdmin(false); + } + // For network errors, keep current state and try again later + } finally { + setIsLoading(false); } }; @@ -48,9 +55,19 @@ function App() { setIsAdmin(false); }; + if (isLoading) { + return ( +
+
+
Loading...
+
+
+ ); + } + return (
-
+
} /> diff --git a/src/services/authService.jsx b/src/services/authService.jsx index 5987ee8..af844a6 100644 --- a/src/services/authService.jsx +++ b/src/services/authService.jsx @@ -12,14 +12,17 @@ class AuthService { }); - // Handle 403 responses (invalid token) by logging out + // Handle authentication errors (401/403) but don't auto-redirect on network errors this.api.interceptors.response.use( (response) => response, (error) => { - if (error.response && error.response.status === 403) { - + // Only auto-logout on explicit authentication errors, not network issues + if (error.response && (error.response.status === 401 || error.response.status === 403)) { this.logout(); - window.location.href = '/auth'; // Assuming there's an auth page + // Only redirect if not already on auth page to avoid redirect loops + if (window.location.pathname !== '/auth') { + window.location.href = '/auth'; + } } return Promise.reject(error); }