gopal rochy
This commit is contained in:
20
src/App.jsx
20
src/App.jsx
@@ -28,6 +28,14 @@ function App() {
|
||||
|
||||
try {
|
||||
console.log('Fetching user profile...');
|
||||
// First check if we might have valid cookies
|
||||
if (!authService.hasValidCookies()) {
|
||||
console.log('No authentication cookies found - skipping profile fetch');
|
||||
setUser(null);
|
||||
setIsAdmin(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await authService.getProfile();
|
||||
console.log('Profile response:', response);
|
||||
setUser(response.user);
|
||||
@@ -38,18 +46,20 @@ function App() {
|
||||
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
|
||||
// Handle different types of errors
|
||||
if (error.response && (error.response.status === 401 || error.response.status === 403)) {
|
||||
console.log('Authentication error - logging out');
|
||||
console.log('Authentication error - clearing local state only');
|
||||
setUser(null);
|
||||
setIsAdmin(false);
|
||||
} else if (retryCount < maxRetries) {
|
||||
// For network errors, retry after a short delay
|
||||
// Don't call logout API - just clear local state
|
||||
} else if (!error.response && retryCount < maxRetries) {
|
||||
// Network error (no response) - retry
|
||||
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');
|
||||
console.log('Max retries reached or other error - keeping current auth state');
|
||||
// Don't clear auth state for non-auth errors
|
||||
}
|
||||
} finally {
|
||||
if (retryCount === 0) {
|
||||
|
||||
@@ -62,8 +62,14 @@ class AuthService {
|
||||
}
|
||||
|
||||
async getProfile() {
|
||||
const response = await this.api.get('/auth/profile');
|
||||
return response.data;
|
||||
try {
|
||||
const response = await this.api.get('/auth/profile');
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
// Don't call logout from here - let the App component handle it
|
||||
// This prevents infinite loops when cookies are cleared
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async logout() {
|
||||
@@ -106,6 +112,12 @@ class AuthService {
|
||||
return null; // Cookies are not accessible from client-side JavaScript
|
||||
}
|
||||
|
||||
hasValidCookies() {
|
||||
// Check if authentication cookies might exist
|
||||
// This is a client-side check, actual validation happens on the server
|
||||
return document.cookie.includes('token=') || document.cookie.includes('jwt=');
|
||||
}
|
||||
|
||||
async isAuthenticated() {
|
||||
try {
|
||||
// Check authentication by making a request to a protected endpoint
|
||||
|
||||
Reference in New Issue
Block a user