This commit is contained in:
2025-11-25 00:59:42 +05:30
parent 0950000045
commit 415d38381f

View File

@@ -11,13 +11,15 @@ class AuthService {
withCredentials: true // Enable sending cookies with requests withCredentials: true // Enable sending cookies with requests
}); });
this.isLoggingOut = false;
// Handle authentication errors (401/403) but don't auto-redirect on network errors // Handle authentication errors (401/403) but don't auto-redirect on network errors
this.api.interceptors.response.use( this.api.interceptors.response.use(
(response) => response, (response) => response,
(error) => { (error) => {
// Only auto-logout on explicit authentication errors, not network issues // Only auto-logout on explicit authentication errors, not network issues
if (error.response && (error.response.status === 401 || error.response.status === 403)) { // and prevent infinite loops
if (error.response && (error.response.status === 401 || error.response.status === 403) && !this.isLoggingOut) {
this.logout(); this.logout();
// Only redirect if not already on auth page to avoid redirect loops // Only redirect if not already on auth page to avoid redirect loops
if (window.location.pathname !== '/auth') { if (window.location.pathname !== '/auth') {
@@ -65,11 +67,21 @@ class AuthService {
} }
async logout() { async logout() {
if (this.isLoggingOut) {
return; // Prevent multiple logout calls
}
this.isLoggingOut = true;
try { try {
await this.api.post('/auth/logout'); await this.api.post('/auth/logout');
// Cookies are cleared by backend; no local storage to clear since we avoid storing tokens in dev. // Cookies are cleared by backend; no local storage to clear since we avoid storing tokens in dev.
} catch (error) { } catch (error) {
console.error('Logout API call failed:', error); console.error('Logout API call failed:', error);
} finally {
this.isLoggingOut = false;
// Clear in-memory tokens
this.clearToken();
} }
} }