Fix jwt-token
This commit is contained in:
@@ -26,17 +26,48 @@ api.interceptors.request.use(
|
|||||||
api.interceptors.response.use(
|
api.interceptors.response.use(
|
||||||
(response) => response,
|
(response) => response,
|
||||||
(error) => {
|
(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;
|
const errorData = error.response.data;
|
||||||
console.warn('🔐 Authentication failed:', errorData?.message || 'Unknown error');
|
console.warn('🔐 Authentication failed:', errorData?.message || 'Unknown error');
|
||||||
|
console.log('🔐 Error details:', {
|
||||||
|
error: errorData?.error,
|
||||||
|
message: errorData?.message,
|
||||||
|
redirectToLogin: errorData?.redirectToLogin
|
||||||
|
});
|
||||||
|
|
||||||
// Clear authentication data
|
// Clear authentication data
|
||||||
|
console.log('🧹 Clearing authentication data...');
|
||||||
localStorage.removeItem('management_token')
|
localStorage.removeItem('management_token')
|
||||||
localStorage.removeItem('management_user')
|
localStorage.removeItem('management_user')
|
||||||
|
|
||||||
// Check if the backend indicates we should redirect to login
|
// Check if the backend indicates we should redirect to login
|
||||||
if (errorData?.redirectToLogin !== false) {
|
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)
|
return Promise.reject(error)
|
||||||
|
|||||||
Reference in New Issue
Block a user