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,5 @@
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') {
@@ -26,18 +27,12 @@ async function handleResponse(response, operation = 'operación') {
return response.json();
}
export async function fetchUsers(token) {
export async function fetchUsers() {
try {
const url = `${API_URL}/user/users/`;
console.log('👥 Fetching users from:', url);
const res = await fetch(url, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
});
const res = await fetchWithAuth(url);
const data = await handleResponse(res, 'Fetch Users');
console.log('✅ Users data received');
@@ -52,20 +47,12 @@ export async function fetchUsers(token) {
}
}
export async function createUser(token, userData) {
export async function createUser(userData) {
try {
const url = `${API_URL}/user/users/`;
console.log(' Creating user at:', url);
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);
const data = await handleResponse(res, 'Create User');
console.log('✅ User created successfully');
@@ -80,20 +67,12 @@ export async function createUser(token, userData) {
}
}
export async function updateUser(token, id, userData) {
export async function updateUser(id, userData) {
try {
const url = `${API_URL}/user/users/${id}/`;
console.log('✏️ Updating user at:', url);
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);
const data = await handleResponse(res, 'Update User');
console.log('✅ User updated successfully');
@@ -108,19 +87,12 @@ export async function updateUser(token, id, userData) {
}
}
export async function deleteUser(token, id) {
export async function deleteUser(id) {
try {
const url = `${API_URL}/user/users/${id}/`;
console.log('🗑️ Deleting user at:', url);
const res = await fetch(url, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
});
const res = await deleteWithAuth(url);
if (res.status === 401) {
console.error('❌ Unauthorized - session expired');
@@ -145,18 +117,12 @@ export async function deleteUser(token, id) {
}
}
export async function getCurrentUser(token) {
export async function getCurrentUser() {
try {
const url = `${API_URL}/user/users/me/`;
console.log('👤 Fetching current user from:', url);
const res = await fetch(url, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
});
const res = await fetchWithAuth(url);
const data = await handleResponse(res, 'Get Current User');
console.log('✅ Current user data received:', data);