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 [isAdmin, setIsAdmin] = useState(false);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
|
||||||
const fetchProfile = async () => {
|
const fetchProfile = async (retryCount = 0) => {
|
||||||
|
const maxRetries = 2;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('Fetching user profile...');
|
||||||
const response = await authService.getProfile();
|
const response = await authService.getProfile();
|
||||||
|
console.log('Profile response:', response);
|
||||||
setUser(response.user);
|
setUser(response.user);
|
||||||
setIsAdmin(response.user.role === 'admin');
|
setIsAdmin(response.user.role === 'admin');
|
||||||
|
console.log('User authenticated:', response.user.email);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch profile:', 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
|
// Only logout on authentication errors (401/403), not network errors
|
||||||
if (error.response && (error.response.status === 401 || error.response.status === 403)) {
|
if (error.response && (error.response.status === 401 || error.response.status === 403)) {
|
||||||
|
console.log('Authentication error - logging out');
|
||||||
setUser(null);
|
setUser(null);
|
||||||
setIsAdmin(false);
|
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 {
|
} finally {
|
||||||
|
if (retryCount === 0) {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// Add a small delay to ensure cookies are properly set after login
|
||||||
|
const timer = setTimeout(() => {
|
||||||
fetchProfile();
|
fetchProfile();
|
||||||
|
}, 100);
|
||||||
|
|
||||||
|
return () => clearTimeout(timer);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleLogin = async () => {
|
const handleLogin = async () => {
|
||||||
await fetchProfile();
|
return fetchProfile();
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLogout = () => {
|
const handleLogout = () => {
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ const Auth = ({ onLogin }) => {
|
|||||||
productService.setToken(response.token);
|
productService.setToken(response.token);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inform parent to refresh profile and update UI
|
// Inform parent to refresh profile and update UI, wait for it to complete
|
||||||
onLogin();
|
await onLogin();
|
||||||
navigate('/');
|
navigate('/');
|
||||||
} else {
|
} else {
|
||||||
if (formData.password !== formData.confirmPassword) {
|
if (formData.password !== formData.confirmPassword) {
|
||||||
|
|||||||
Reference in New Issue
Block a user