- Centralizado client HTTP em apiClient - Padronizado parse de resposta e tratamento de erro - Mantido Bearer Token via interceptor global de fetch - Validado JWT pelo campo exp antes de autenticar sessão - Removidos fallbacks mockados de usuário - Bloqueado fluxo legado de login por query string - Preservada rota original após redirecionamento para login - Protegida rota /admin/whatsapp por perfil admin - Atualizada wiki frontend de autenticação, rotas e services
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
import { API_BASE_URL } from './apiConfig';
|
|
import { clearSession, getAuthToken } from '../../modules/auth/services/sessionService';
|
|
|
|
function shouldAttachToken(resource) {
|
|
const url = typeof resource === 'string' ? resource : resource?.url;
|
|
|
|
return Boolean(url && url.startsWith(API_BASE_URL) && getAuthToken());
|
|
}
|
|
|
|
export function installAuthFetchInterceptor() {
|
|
window.localStorage.removeItem('authToken');
|
|
window.localStorage.removeItem('authUser');
|
|
|
|
const nativeFetch = window.fetch.bind(window);
|
|
|
|
window.fetch = async (resource, options = {}) => {
|
|
if (!shouldAttachToken(resource)) {
|
|
return nativeFetch(resource, options);
|
|
}
|
|
|
|
const headers = new Headers(options.headers || {});
|
|
|
|
if (!headers.has('Authorization')) {
|
|
headers.set('Authorization', `Bearer ${getAuthToken()}`);
|
|
}
|
|
|
|
const response = await nativeFetch(resource, {
|
|
...options,
|
|
headers,
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
clearSession();
|
|
|
|
if (window.location.pathname !== '/login') {
|
|
window.location.assign('/login');
|
|
}
|
|
}
|
|
|
|
return response;
|
|
};
|
|
}
|