fix
This commit is contained in:
28
src/App.jsx
28
src/App.jsx
@@ -23,30 +23,52 @@ function App() {
|
||||
const [isAdmin, setIsAdmin] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
const fetchProfile = async () => {
|
||||
const fetchProfile = async (retryCount = 0) => {
|
||||
const maxRetries = 2;
|
||||
|
||||
try {
|
||||
console.log('Fetching user profile...');
|
||||
const response = await authService.getProfile();
|
||||
console.log('Profile response:', response);
|
||||
setUser(response.user);
|
||||
setIsAdmin(response.user.role === 'admin');
|
||||
console.log('User authenticated:', response.user.email);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch profile:', error);
|
||||
console.error('Error status:', error.response?.status);
|
||||
console.error('Error message:', error.response?.data?.message || error.message);
|
||||
|
||||
// Only logout on authentication errors (401/403), not network errors
|
||||
if (error.response && (error.response.status === 401 || error.response.status === 403)) {
|
||||
console.log('Authentication error - logging out');
|
||||
setUser(null);
|
||||
setIsAdmin(false);
|
||||
} else if (retryCount < maxRetries) {
|
||||
// For network errors, retry after a short delay
|
||||
console.log(`Network error - retrying (${retryCount + 1}/${maxRetries})`);
|
||||
setTimeout(() => fetchProfile(retryCount + 1), 1000);
|
||||
return; // Don't set loading to false yet
|
||||
} else {
|
||||
console.log('Max retries reached - keeping current auth state');
|
||||
}
|
||||
// For network errors, keep current state and try again later
|
||||
} finally {
|
||||
if (retryCount === 0) {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
// Add a small delay to ensure cookies are properly set after login
|
||||
const timer = setTimeout(() => {
|
||||
fetchProfile();
|
||||
}, 100);
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}, []);
|
||||
|
||||
const handleLogin = async () => {
|
||||
await fetchProfile();
|
||||
return fetchProfile();
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
|
||||
@@ -44,8 +44,8 @@ const Auth = ({ onLogin }) => {
|
||||
productService.setToken(response.token);
|
||||
}
|
||||
|
||||
// Inform parent to refresh profile and update UI
|
||||
onLogin();
|
||||
// Inform parent to refresh profile and update UI, wait for it to complete
|
||||
await onLogin();
|
||||
navigate('/');
|
||||
} else {
|
||||
if (formData.password !== formData.confirmPassword) {
|
||||
|
||||
Reference in New Issue
Block a user