Improve the design of the frontend 2
This commit is contained in:
@@ -34,23 +34,8 @@ class AuthInterceptor {
|
||||
},
|
||||
});
|
||||
|
||||
apiClient.addResponseInterceptor({
|
||||
onResponseError: async (error: any) => {
|
||||
// Handle 401 Unauthorized - redirect to login
|
||||
if (error?.response?.status === 401) {
|
||||
localStorage.removeItem('auth_token');
|
||||
localStorage.removeItem('refresh_token');
|
||||
localStorage.removeItem('user_data');
|
||||
|
||||
// Redirect to login page
|
||||
if (typeof window !== 'undefined') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
|
||||
throw error;
|
||||
},
|
||||
});
|
||||
// Note: 401 handling is now managed by ErrorRecoveryInterceptor
|
||||
// This allows token refresh to work before redirecting to login
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,7 +182,8 @@ class ErrorRecoveryInterceptor {
|
||||
}
|
||||
|
||||
// Attempt to refresh token
|
||||
const response = await fetch(`${apiClient['baseURL']}/auth/refresh`, {
|
||||
const baseURL = (apiClient as any).baseURL || window.location.origin;
|
||||
const response = await fetch(`${baseURL}/api/v1/auth/refresh`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -347,19 +333,24 @@ class PerformanceInterceptor {
|
||||
|
||||
/**
|
||||
* Setup all interceptors
|
||||
* IMPORTANT: Order matters! ErrorRecoveryInterceptor must be first to handle token refresh
|
||||
*/
|
||||
export const setupInterceptors = () => {
|
||||
// 1. Error recovery first (handles 401 and token refresh)
|
||||
ErrorRecoveryInterceptor.setup();
|
||||
|
||||
// 2. Authentication (adds Bearer tokens)
|
||||
AuthInterceptor.setup();
|
||||
|
||||
const isDevelopment = import.meta.env.DEV;
|
||||
|
||||
// 3. Tenant context
|
||||
TenantInterceptor.setup();
|
||||
|
||||
// 4. Development-only interceptors
|
||||
const isDevelopment = true; // Temporarily set to true for development
|
||||
if (isDevelopment) {
|
||||
LoggingInterceptor.setup();
|
||||
PerformanceInterceptor.setup();
|
||||
}
|
||||
|
||||
TenantInterceptor.setup();
|
||||
ErrorRecoveryInterceptor.setup();
|
||||
};
|
||||
|
||||
// Export interceptor classes for manual setup if needed
|
||||
|
||||
Reference in New Issue
Block a user