Se soluciono autenticacion

This commit is contained in:
2025-08-05 13:06:24 -06:00
parent c280afe646
commit c9df4e3ab2
21 changed files with 758 additions and 624 deletions

View File

@@ -1,4 +1,7 @@
const API_URL = import.meta.env.VITE_EFC_API_URL || 'http://localhost:8000';
import { fetchWithAuth, postWithAuth, putWithAuth, deleteWithAuth } from '../fetchWithAuth';
// Función helper para manejar respuestas
async function handleResponse(response, operation = 'operación') {
if (response.status === 401) {
@@ -15,57 +18,27 @@ async function handleResponse(response, operation = 'operación') {
return response.json();
}
export async function fetchUsers(token) {
export async function fetchUsers() {
const url = `${API_URL}/user/users/`;
const res = await fetch(url, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
});
const res = await fetchWithAuth(url);
return handleResponse(res, 'Fetch Users');
}
export async function createUser(token, userData) {
export async function createUser(userData) {
const url = `${API_URL}/user/users/`;
const res = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify(userData),
});
const res = await postWithAuth(url, userData);
return handleResponse(res, 'Create User');
}
export async function updateUser(token, id, userData) {
export async function updateUser(id, userData) {
const url = `${API_URL}/user/users/${id}/`;
const res = await fetch(url, {
method: 'PUT',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify(userData),
});
const res = await putWithAuth(url, userData);
return handleResponse(res, 'Update User');
}
export async function deleteUser(token, id) {
export async function deleteUser(id) {
const url = `${API_URL}/user/users/${id}/`;
const res = await fetch(url, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
});
if (res.status === 401) throw new Error('SESSION_EXPIRED');
const res = await deleteWithAuth(url);
if (!res.ok) throw new Error(`Error ${res.status}: ${res.statusText}`);
return true;
}