refactor on components and delete clear, profesional login
This commit is contained in:
@@ -7,15 +7,18 @@ import Favorites from './views/Favorites.vue';
|
||||
import Workers from './views/Workers.vue';
|
||||
import Users from './views/Users.vue';
|
||||
import Logs from './views/Logs.vue';
|
||||
import Login from './views/Login.vue';
|
||||
import './style.css';
|
||||
import authService from './services/auth';
|
||||
|
||||
const routes = [
|
||||
{ path: '/', component: Dashboard },
|
||||
{ path: '/articles', component: Articles },
|
||||
{ path: '/favorites', component: Favorites },
|
||||
{ path: '/workers', component: Workers },
|
||||
{ path: '/users', component: Users },
|
||||
{ path: '/logs', component: Logs },
|
||||
{ path: '/login', component: Login, name: 'login' },
|
||||
{ path: '/', component: Dashboard, meta: { requiresAuth: true } },
|
||||
{ path: '/articles', component: Articles, meta: { requiresAuth: true } },
|
||||
{ path: '/favorites', component: Favorites, meta: { requiresAuth: true } },
|
||||
{ path: '/workers', component: Workers, meta: { requiresAuth: true } },
|
||||
{ path: '/users', component: Users, meta: { requiresAuth: true } },
|
||||
{ path: '/logs', component: Logs, meta: { requiresAuth: true } },
|
||||
];
|
||||
|
||||
const router = createRouter({
|
||||
@@ -23,6 +26,43 @@ const router = createRouter({
|
||||
routes,
|
||||
});
|
||||
|
||||
// Guard de navegación para verificar autenticación
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
// Si la ruta es /login y ya está autenticado, redirigir al dashboard
|
||||
if (to.path === '/login') {
|
||||
if (authService.hasCredentials()) {
|
||||
const isValid = await authService.validateSession();
|
||||
if (isValid) {
|
||||
next('/');
|
||||
return;
|
||||
}
|
||||
}
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
// Para todas las demás rutas, verificar autenticación
|
||||
if (to.meta.requiresAuth) {
|
||||
// Verificar si hay token almacenado
|
||||
if (!authService.hasCredentials()) {
|
||||
// No hay token, redirigir a login
|
||||
next('/login');
|
||||
return;
|
||||
}
|
||||
|
||||
// Hay token, validar si sigue siendo válido
|
||||
const isValid = await authService.validateSession();
|
||||
if (!isValid) {
|
||||
// Token inválido o expirado, redirigir a login
|
||||
next('/login');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Continuar la navegación
|
||||
next();
|
||||
});
|
||||
|
||||
const app = createApp(App);
|
||||
app.use(router);
|
||||
app.mount('#app');
|
||||
|
||||
Reference in New Issue
Block a user