fixed
This commit is contained in:
23
src/App.jsx
23
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 (
|
||||
<div className="App">
|
||||
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh' }}>
|
||||
<div>Loading...</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
<Header user={user} onLogout={handleLogout} isAdmin={isAdmin} />
|
||||
<Header user={user} onLogout={handleLogout} isAdmin={isLoading ? false : isAdmin} />
|
||||
<main>
|
||||
<Routes>
|
||||
<Route path="/" element={<Home />} />
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user