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