fix: admin-clinics redirecionar para login se sem token + verificar content-type
continuous-integration/webhook Deploy concluido (rx.scoreodonto.com)
continuous-integration/webhook Deploy concluido (rx.scoreodonto.com)
This commit is contained in:
@@ -2,12 +2,15 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
// Configurações
|
||||
const API_URL = '/api/admin/clinics';
|
||||
|
||||
// Pegar Token JWT do Admin (salvo no localStorage ou cookies)
|
||||
// Assumindo que a sessão já está lidada por cookies ou podemos buscar da rota,
|
||||
// mas usaremos credenciais padrão do fetch se usar cookie de sessão.
|
||||
// Se usar JWT localStorage:
|
||||
// Pegar Token JWT do Admin
|
||||
const token = localStorage.getItem('token') || '';
|
||||
|
||||
// Se não tem token, redirecionar imediatamente para login
|
||||
if (!token) {
|
||||
window.location.href = '/login';
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchOptions = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -40,12 +43,22 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
async function loadClinics() {
|
||||
try {
|
||||
const response = await fetch(API_URL, fetchOptions);
|
||||
if (!response.ok) {
|
||||
|
||||
// Se recebeu 401 ou 403, token expirou — vai para login
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
localStorage.removeItem('token');
|
||||
window.location.href = '/login';
|
||||
return;
|
||||
}
|
||||
throw new Error('Falha ao buscar clínicas');
|
||||
|
||||
// Verificar se a resposta é realmente JSON (evita erro com HTML)
|
||||
const contentType = response.headers.get('content-type') || '';
|
||||
if (!contentType.includes('application/json')) {
|
||||
throw new Error('Sessão expirada ou servidor indisponível. Faça login novamente.');
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Erro HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
@@ -57,7 +70,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
clinicsTableBody.innerHTML = `<tr><td colspan="7" style="text-align:center;color:red;">Erro ao carregar dados.</td></tr>`;
|
||||
clinicsTableBody.innerHTML = `<tr><td colspan="7" style="text-align:center;color:red;">${error.message || 'Erro ao carregar dados.'}</td></tr>`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user