This commit is contained in:
Jake Kasper
2025-09-04 07:36:07 -05:00
parent 9a1640af37
commit fcce8ba86d

View File

@@ -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