asdfasdf
This commit is contained in:
@@ -41,17 +41,27 @@ apiClient.interceptors.request.use(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Response interceptor for error handling
|
// Response interceptor for error handling
|
||||||
|
let redirectingToLogin = false;
|
||||||
apiClient.interceptors.response.use(
|
apiClient.interceptors.response.use(
|
||||||
(response) => response,
|
(response) => response,
|
||||||
(error) => {
|
(error) => {
|
||||||
// Handle specific error codes
|
// Handle specific error codes
|
||||||
if (error.response?.status === 401 || error.response?.status === 403) {
|
if (error.response?.status === 401 || error.response?.status === 403) {
|
||||||
// Unauthorized or malformed token - clear token and redirect to login
|
// Unauthorized or malformed token - clear token and redirect to login
|
||||||
console.log('Clearing invalid token due to auth error:', error.response?.status);
|
const reqUrl = error.config?.url || '';
|
||||||
localStorage.removeItem('authToken');
|
const isPublicAuthCheck = reqUrl.includes('/auth/registration-status');
|
||||||
// Use React Router navigation instead of hard redirect
|
const onAuthPage = ['/login','/register'].includes(window.location.pathname);
|
||||||
if (window.location.pathname !== '/login' && window.location.pathname !== '/register') {
|
if (!isPublicAuthCheck) {
|
||||||
window.location.href = '/login';
|
console.log('Clearing invalid token due to auth error:', error.response?.status, 'on', reqUrl);
|
||||||
|
localStorage.removeItem('authToken');
|
||||||
|
if (!onAuthPage && !redirectingToLogin) {
|
||||||
|
redirectingToLogin = true;
|
||||||
|
try {
|
||||||
|
window.location.href = '/login';
|
||||||
|
} finally {
|
||||||
|
setTimeout(()=>{ redirectingToLogin = false; }, 1500);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (error.response?.status >= 500) {
|
} else if (error.response?.status >= 500) {
|
||||||
// Server error
|
// Server error
|
||||||
|
|||||||
Reference in New Issue
Block a user