feat(Login): add example jwt token

This commit is contained in:
2026-02-14 17:49:49 -05:00
parent c593dd0273
commit 173ddfd05f
6 changed files with 160 additions and 34 deletions

49
src/services/auth.js Normal file
View File

@@ -0,0 +1,49 @@
class AuthService {
static TOKEN_KEY = 'access_token';
static REFRESH_KEY = 'refresh_token';
static login(credentials) {
const url = `${import.meta.env.VITE_DJANGO_BASE_URL}/api/token/`;
return fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(credentials),
})
.then(r => r.json())
.then(data => {
localStorage.setItem(this.TOKEN_KEY, data.access);
localStorage.setItem(this.REFRESH_KEY, data.refresh);
return data;
});
}
static getAccessToken() {
return localStorage.getItem(this.TOKEN_KEY);
}
static getRefreshToken() {
return localStorage.getItem(this.REFRESH_KEY);
}
static async refresh() {
const refresh = this.getRefreshToken();
if (!refresh) throw new Error('No refresh token');
const url = `${import.meta.env.VITE_DJANGO_BASE_URL}/api/token/refresh/`;
const resp = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ refresh }),
});
const data = await resp.json();
localStorage.setItem(this.TOKEN_KEY, data.access);
return data.access;
}
static logout() {
localStorage.removeItem(this.TOKEN_KEY);
localStorage.removeItem(this.REFRESH_KEY);
}
}
export default AuthService;

View File

@@ -1,8 +1,38 @@
import AuthService from '@/services/auth';
class DjangoApi {
constructor() {
this.base = import.meta.env.VITE_DJANGO_BASE_URL;
}
_authHeaders() {
const token = AuthService.getAccessToken();
return token ? { Authorization: `Bearer ${token}` } : {};
}
_handleResponse(response) {
if (!response.ok) {
// Si el token ha expirado (401) intentamos refrescar y reintentar
if (response.status === 401) {
return AuthService.refresh().then(newToken => {
// volver a ejecutar la petición original con el nuevo token
const retryHeaders = {
...response.headers,
Authorization: `Bearer ${newToken}`,
};
// aquí usamos fetch de nuevo con los mismos parámetros
// (para simplificar, delegamos a getRequest/postRequest)
// En la práctica, extrae la lógica a una función reutilizable.
throw new Error('Retry logic should be implemented here');
});
}
return response.json().then(err => {
throw new Error(`Error ${response.status}: ${err.detail || response.statusText}`);
});
}
return response.json();
}
getCustomers() {
const url = this.base + '/don_confiao/api/customers/';
return this.getRequest(url);
@@ -79,42 +109,23 @@ class DjangoApi {
}
getRequest(url) {
return new Promise ((resolve, reject) => {
fetch(url)
.then(response => response.json())
.then(data => {
resolve(data);
})
.catch(error => {
reject(error);
});
});
return fetch(url, {
headers: {
'Content-Type': 'application/json',
...this._authHeaders(),
},
}).then(this._handleResponse);
}
postRequest(url, content) {
return new Promise((resolve, reject) => {
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(content)
})
.then(response => {
if (!response.ok) {
reject(new Error(`Error ${response.status}: ${response.statusText}`));
} else {
response.json().then(data => {
if (!data) {
reject(new Error('La respuesta no es un JSON válido'));
} else {
resolve(data);
}
});
}
})
.catch(error => reject(error));
});
return fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...this._authHeaders(),
},
body: JSON.stringify(content),
}).then(this._handleResponse);
}
}