This commit is contained in:
2025-11-25 00:55:21 +05:30
parent 46a70b6e8d
commit 0950000045
2 changed files with 29 additions and 7 deletions

View File

@@ -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 {
setIsLoading(false);
if (retryCount === 0) {
setIsLoading(false);
}
}
};
useEffect(() => {
fetchProfile();
// 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 = () => {

View File

@@ -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) {