gopal rochy
This commit is contained in:
20
src/App.jsx
20
src/App.jsx
@@ -28,6 +28,14 @@ function App() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('Fetching user profile...');
|
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();
|
const response = await authService.getProfile();
|
||||||
console.log('Profile response:', response);
|
console.log('Profile response:', response);
|
||||||
setUser(response.user);
|
setUser(response.user);
|
||||||
@@ -38,18 +46,20 @@ function App() {
|
|||||||
console.error('Error status:', error.response?.status);
|
console.error('Error status:', error.response?.status);
|
||||||
console.error('Error message:', error.response?.data?.message || error.message);
|
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)) {
|
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);
|
setUser(null);
|
||||||
setIsAdmin(false);
|
setIsAdmin(false);
|
||||||
} else if (retryCount < maxRetries) {
|
// Don't call logout API - just clear local state
|
||||||
// For network errors, retry after a short delay
|
} else if (!error.response && retryCount < maxRetries) {
|
||||||
|
// Network error (no response) - retry
|
||||||
console.log(`Network error - retrying (${retryCount + 1}/${maxRetries})`);
|
console.log(`Network error - retrying (${retryCount + 1}/${maxRetries})`);
|
||||||
setTimeout(() => fetchProfile(retryCount + 1), 1000);
|
setTimeout(() => fetchProfile(retryCount + 1), 1000);
|
||||||
return; // Don't set loading to false yet
|
return; // Don't set loading to false yet
|
||||||
} else {
|
} 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 {
|
} finally {
|
||||||
if (retryCount === 0) {
|
if (retryCount === 0) {
|
||||||
|
|||||||
@@ -62,8 +62,14 @@ class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getProfile() {
|
async getProfile() {
|
||||||
const response = await this.api.get('/auth/profile');
|
try {
|
||||||
return response.data;
|
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() {
|
async logout() {
|
||||||
@@ -106,6 +112,12 @@ class AuthService {
|
|||||||
return null; // Cookies are not accessible from client-side JavaScript
|
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() {
|
async isAuthenticated() {
|
||||||
try {
|
try {
|
||||||
// Check authentication by making a request to a protected endpoint
|
// Check authentication by making a request to a protected endpoint
|
||||||
|
|||||||
Reference in New Issue
Block a user