90 lines
2.8 KiB
JavaScript
90 lines
2.8 KiB
JavaScript
import { createApp } from 'vue';
|
|
import { createRouter, createWebHistory } from 'vue-router';
|
|
import App from './App.vue';
|
|
import Dashboard from './views/Dashboard.vue';
|
|
import Articles from './views/Articles.vue';
|
|
import ArticleDetail from './views/ArticleDetail.vue';
|
|
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 RateLimiter from './views/RateLimiter.vue';
|
|
import Sessions from './views/Sessions.vue';
|
|
import Login from './views/Login.vue';
|
|
import './style.css';
|
|
import authService from './services/auth';
|
|
|
|
const routes = [
|
|
{ path: '/login', component: Login, name: 'login' },
|
|
{ path: '/', component: Dashboard, meta: { requiresAuth: true } },
|
|
{ path: '/articles', component: Articles, meta: { requiresAuth: true } },
|
|
{ path: '/articles/:platform/:id', component: ArticleDetail, 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 } },
|
|
{ path: '/rate-limiter', component: RateLimiter, meta: { requiresAuth: true } },
|
|
{ path: '/sessions', component: Sessions, meta: { requiresAuth: true } },
|
|
];
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
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');
|
|
|
|
// Registrar Service Worker automáticamente al cargar la app
|
|
if ('serviceWorker' in navigator) {
|
|
window.addEventListener('load', async () => {
|
|
try {
|
|
const registration = await navigator.serviceWorker.register('/sw.js', {
|
|
scope: '/'
|
|
});
|
|
console.log('Service Worker registrado:', registration.scope);
|
|
} catch (error) {
|
|
console.error('Error registrando Service Worker:', error);
|
|
}
|
|
});
|
|
}
|
|
|