Merge pull request 'Implementando autenticación usando jwt #28' (#31) from implement_jwt_authentication_#28 into main
Reviewed-on: #31
This commit is contained in:
70
src/components/Login.vue
Normal file
70
src/components/Login.vue
Normal file
@@ -0,0 +1,70 @@
|
||||
<template>
|
||||
<h1>Login</h1>
|
||||
|
||||
<v-form ref="loginForm" @submit.prevent="onSubmit">
|
||||
<v-text-field
|
||||
v-model="username"
|
||||
label="Usuario"
|
||||
:rules="[requiredRule]"
|
||||
required
|
||||
/>
|
||||
<v-text-field
|
||||
v-model="password"
|
||||
label="Contraseña"
|
||||
type="password"
|
||||
:rules="[requiredRule]"
|
||||
required
|
||||
/>
|
||||
|
||||
<v-btn type="submit" color="primary">Entrar</v-btn>
|
||||
|
||||
<v-alert v-if="error" type="error" class="mt-2">{{ error }}</v-alert>
|
||||
</v-form>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import AuthService from '@/services/auth';
|
||||
|
||||
export default {
|
||||
name: 'DonConfiao',
|
||||
|
||||
data() {
|
||||
return {
|
||||
username: '',
|
||||
password: '',
|
||||
error: '',
|
||||
};
|
||||
},
|
||||
|
||||
methods: {
|
||||
requiredRule(value) {
|
||||
return !!value || 'Este campo es obligatorio';
|
||||
},
|
||||
|
||||
async onSubmit() {
|
||||
this.error = '';
|
||||
|
||||
const form = this.$refs.loginForm;
|
||||
const isValid = await form.validate();
|
||||
|
||||
if (!isValid) return;
|
||||
|
||||
if (!this.username || !this.password) {
|
||||
this.error = 'Usuario y contraseña son obligatorios';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await AuthService.login({
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
});
|
||||
this.$router.push({ path: '/' });
|
||||
} catch (e) {
|
||||
const msg = e?.response?.data?.message ?? e.message;
|
||||
this.error = msg ?? 'Error al iniciar sesión';
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
69
src/components/LoginDialog.vue
Normal file
69
src/components/LoginDialog.vue
Normal file
@@ -0,0 +1,69 @@
|
||||
<template>
|
||||
<v-dialog v-model="show" max-width="400">
|
||||
<v-card>
|
||||
<v-card-title class="headline">Iniciar sesión</v-card-title>
|
||||
<v-card-text>
|
||||
<v-form ref="form" @submit.prevent="onSubmit">
|
||||
<v-text-field
|
||||
v-model="username"
|
||||
label="Usuario"
|
||||
:rules="[required]"
|
||||
required
|
||||
/>
|
||||
<v-text-field
|
||||
v-model="password"
|
||||
label="Contraseña"
|
||||
type="password"
|
||||
:rules="[required]"
|
||||
required
|
||||
/>
|
||||
<v-alert v-if="error" type="error" class="mt-2">{{ error }}</v-alert>
|
||||
</v-form>
|
||||
</v-card-text>
|
||||
|
||||
<v-card-actions>
|
||||
<v-spacer />
|
||||
<v-btn text @click="show = false">Cancelar</v-btn>
|
||||
<v-btn color="primary" @click="onSubmit">Entrar</v-btn>
|
||||
</v-card-actions>
|
||||
</v-card>
|
||||
</v-dialog>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import AuthService from '@/services/auth';
|
||||
|
||||
export default {
|
||||
name: 'LoginDialog',
|
||||
data: () => ({
|
||||
show: false,
|
||||
username: '',
|
||||
password: '',
|
||||
error: '',
|
||||
}),
|
||||
methods: {
|
||||
required(v) {
|
||||
return !!v || 'Campo obligatorio';
|
||||
},
|
||||
async onSubmit() {
|
||||
this.error = '';
|
||||
const form = this.$refs.form;
|
||||
if (!(await form.validate())) return;
|
||||
|
||||
try {
|
||||
await AuthService.login({
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
});
|
||||
this.show = false;
|
||||
this.$emit('login-success');
|
||||
} catch (e) {
|
||||
this.error = e.message ?? 'Error al iniciar sesión';
|
||||
}
|
||||
},
|
||||
open() {
|
||||
this.show = true;
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
32
src/components/Logout.vue
Normal file
32
src/components/Logout.vue
Normal file
@@ -0,0 +1,32 @@
|
||||
<template>
|
||||
<v-container class="d-flex flex-column align-center justify-center" style="height: 100vh;">
|
||||
<v-progress-circular indeterminate color="primary" />
|
||||
<p class="mt-4">Cerrando sesión…</p>
|
||||
</v-container>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import AuthService from '@/services/auth';
|
||||
|
||||
export default {
|
||||
name: 'DonConfiao',
|
||||
mounted() {
|
||||
this.logout();
|
||||
},
|
||||
methods: {
|
||||
logout() {
|
||||
AuthService.logout();
|
||||
this.$router.push({
|
||||
path: '/autenticarse'
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
p {
|
||||
font-size: 1.1rem;
|
||||
color: #555;
|
||||
}
|
||||
</style>
|
||||
@@ -3,11 +3,22 @@
|
||||
<v-app-bar-nav-icon variant="text" @click.stop="drawer = !drawer"></v-app-bar-nav-icon>
|
||||
<v-toolbar-title>Menu</v-toolbar-title>
|
||||
<v-spacer></v-spacer>
|
||||
<template v-if="$vuetify.display.mdAndUp">
|
||||
<v-btn icon="mdi-magnify" variant="text"></v-btn>
|
||||
<v-btn icon="mdi-filter" variant="text"></v-btn>
|
||||
</template>
|
||||
<v-btn icon="mdi-dots-vertical" variant="text"></v-btn>
|
||||
<v-btn
|
||||
v-if="!isAuthenticated"
|
||||
prepend-icon="mdi-login"
|
||||
variant="text"
|
||||
@click="navigate('/autenticarse')"
|
||||
>
|
||||
Login
|
||||
</v-btn>
|
||||
<v-btn
|
||||
v-else
|
||||
prepend-icon="mdi-logout"
|
||||
variant="text"
|
||||
@click="logout"
|
||||
>
|
||||
Logout
|
||||
</v-btn>
|
||||
</v-app-bar>
|
||||
<v-navigation-drawer v-model="drawer"
|
||||
:location="$vuetify.display.mobile ? 'bottom' : undefined"
|
||||
@@ -41,12 +52,14 @@
|
||||
|
||||
<script>
|
||||
import trytonIcon from '../assets/icons/tryton-icon.svg';
|
||||
import AuthService from '@/services/auth';
|
||||
export default {
|
||||
name: 'NavBar',
|
||||
data: () => ({
|
||||
drawer: false,
|
||||
group: null,
|
||||
showAdminMenu: false,
|
||||
isAuthenticated: false,
|
||||
menuItems: [
|
||||
{ title: 'Inicio', route: '/', icon: 'mdi-home'},
|
||||
{ title: 'Comprar', route:'/comprar', icon: 'mdi-cart'},
|
||||
@@ -61,12 +74,21 @@
|
||||
{ title: 'Actualizar Ventas Tryton', route: '/sincronizar_ventas_tryton', icon: 'trytonIcon'}
|
||||
],
|
||||
}),
|
||||
mounted() {
|
||||
this.checkAuth();
|
||||
},
|
||||
watch: {
|
||||
group () {
|
||||
this.drawer = false
|
||||
},
|
||||
$route() {
|
||||
this.checkAuth();
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
checkAuth() {
|
||||
this.isAuthenticated = AuthService.isAuthenticated();
|
||||
},
|
||||
navigate(route) {
|
||||
this.$router.push(route);
|
||||
},
|
||||
@@ -77,6 +99,11 @@
|
||||
toggleAdminMenu() {
|
||||
this.showAdminMenu = !this.showAdminMenu;
|
||||
},
|
||||
logout() {
|
||||
AuthService.logout();
|
||||
this.isAuthenticated = false;
|
||||
this.$router.push('/');
|
||||
},
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</script>
|
||||
|
||||
7
src/pages/autenticarse.vue
Normal file
7
src/pages/autenticarse.vue
Normal file
@@ -0,0 +1,7 @@
|
||||
<template>
|
||||
<Login />
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
//
|
||||
</script>
|
||||
7
src/pages/salir.vue
Normal file
7
src/pages/salir.vue
Normal file
@@ -0,0 +1,7 @@
|
||||
<template>
|
||||
<Logout />
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
//
|
||||
</script>
|
||||
72
src/services/auth.js
Normal file
72
src/services/auth.js
Normal file
@@ -0,0 +1,72 @@
|
||||
class AuthService {
|
||||
static TOKEN_KEY = 'access_token';
|
||||
static REFRESH_KEY = 'refresh_token';
|
||||
|
||||
static async login(credentials) {
|
||||
const url = `${import.meta.env.VITE_DJANGO_BASE_URL}/api/token/`;
|
||||
|
||||
const resp = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(credentials),
|
||||
});
|
||||
|
||||
if (!resp.ok) {
|
||||
let errMsg = resp.statusText;
|
||||
try {
|
||||
const errData = await resp.json();
|
||||
errMsg = errData?.detail ?? errData?.message ?? errMsg;
|
||||
} catch (_) { /* ignore */ }
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
const data = await resp.json();
|
||||
|
||||
if (data.access && data.refresh) {
|
||||
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 }),
|
||||
});
|
||||
|
||||
if (!resp.ok) {
|
||||
const errData = await resp.json().catch(() => ({}));
|
||||
throw new Error(errData?.detail ?? resp.statusText);
|
||||
}
|
||||
|
||||
const data = await resp.json();
|
||||
localStorage.setItem(this.TOKEN_KEY, data.access);
|
||||
return data.access;
|
||||
}
|
||||
|
||||
static isAuthenticated() {
|
||||
return !!this.getAccessToken();
|
||||
}
|
||||
|
||||
static logout() {
|
||||
localStorage.removeItem(this.TOKEN_KEY);
|
||||
localStorage.removeItem(this.REFRESH_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
export default AuthService;
|
||||
@@ -1,8 +1,19 @@
|
||||
import AuthService from '@/services/auth';
|
||||
import http from '@/services/http';
|
||||
|
||||
class DjangoApi {
|
||||
constructor() {
|
||||
this.base = import.meta.env.VITE_DJANGO_BASE_URL;
|
||||
}
|
||||
|
||||
getRequest(url) {
|
||||
return http.get(url).then(r => r.data);
|
||||
}
|
||||
|
||||
postRequest(url, payload) {
|
||||
return http.post(url, payload).then(r => r.data);
|
||||
}
|
||||
|
||||
getCustomers() {
|
||||
const url = this.base + '/don_confiao/api/customers/';
|
||||
return this.getRequest(url);
|
||||
@@ -77,45 +88,6 @@ class DjangoApi {
|
||||
const url = this.base + '/don_confiao/api/enviar_ventas_a_tryton';
|
||||
return this.postRequest(url, {});
|
||||
}
|
||||
|
||||
getRequest(url) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
fetch(url)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
resolve(data);
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default DjangoApi;
|
||||
export default DjangoApi;
|
||||
|
||||
44
src/services/http.js
Normal file
44
src/services/http.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import axios from 'axios';
|
||||
import AuthService from '@/services/auth';
|
||||
|
||||
const http = axios.create({
|
||||
baseURL: import.meta.env.VITE_DJANGO_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
http.interceptors.request.use(
|
||||
config => {
|
||||
const token = AuthService.getAccessToken();
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
error => Promise.reject(error)
|
||||
);
|
||||
|
||||
http.interceptors.response.use(
|
||||
response => response,
|
||||
async error => {
|
||||
const originalRequest = error.config;
|
||||
|
||||
if (error.response?.status === 401 && !originalRequest._retry) {
|
||||
originalRequest._retry = true;
|
||||
try {
|
||||
const newAccess = await AuthService.refresh();
|
||||
originalRequest.headers.Authorization = `Bearer ${newAccess}`;
|
||||
return http.request(originalRequest);
|
||||
} catch (refreshError) {
|
||||
AuthService.logout();
|
||||
window.location.href = '/autenticarse';
|
||||
return Promise.reject(refreshError);
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default http;
|
||||
Reference in New Issue
Block a user