Fix jwt-token

This commit is contained in:
2025-09-19 07:24:42 +02:00
parent 547b29af78
commit a575e39970

View File

@@ -26,17 +26,48 @@ api.interceptors.request.use(
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
console.log('🚨 API Response Error:', {
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data,
config: {
url: error.config?.url,
method: error.config?.method
}
});
if (error.response?.status === 401 || error.response?.status === 403) {
const errorData = error.response.data;
console.warn('🔐 Authentication failed:', errorData?.message || 'Unknown error');
console.log('🔐 Error details:', {
error: errorData?.error,
message: errorData?.message,
redirectToLogin: errorData?.redirectToLogin
});
// Clear authentication data
console.log('🧹 Clearing authentication data...');
localStorage.removeItem('management_token')
localStorage.removeItem('management_user')
// Check if the backend indicates we should redirect to login
if (errorData?.redirectToLogin !== false) {
window.location.href = '/login'
console.log('🔄 Redirecting to login page...');
// Use both methods to ensure redirect works
try {
if (window.location.pathname !== '/login') {
console.log('🔄 Current path:', window.location.pathname, '- redirecting...');
window.location.href = '/login';
} else {
console.log('🔄 Already on login page, skipping redirect');
}
} catch (e) {
console.error('Failed to redirect via location.href:', e);
// Fallback: try replace
window.location.replace('/login');
}
} else {
console.log('🚫 Redirect to login disabled by backend response');
}
}
return Promise.reject(error)