2 Commits

Author SHA1 Message Date
mono
c1fccc4d53 feat: add admin route protection via router beforeEach 2026-03-15 23:46:08 -05:00
mono
eb4811ab8b feat: add auth protection to routes using meta fields 2026-03-15 23:32:36 -05:00
4 changed files with 40 additions and 2 deletions

View File

@@ -3,5 +3,9 @@
</template> </template>
<script setup> <script setup>
// definePage({
meta: {
requiresAuth: true
}
})
</script> </script>

View File

@@ -3,5 +3,9 @@
</template> </template>
<script setup> <script setup>
// definePage({
meta: {
requiresAuth: true
}
})
</script> </script>

View File

@@ -9,12 +9,39 @@
import { createRouter, createWebHistory } from 'vue-router/auto' import { createRouter, createWebHistory } from 'vue-router/auto'
import { setupLayouts } from 'virtual:generated-layouts' import { setupLayouts } from 'virtual:generated-layouts'
import { routes } from 'vue-router/auto-routes' import { routes } from 'vue-router/auto-routes'
import { useAuthStore } from '@/stores/auth'
const ADMIN_ROUTES = [
'/sincronizar_clientes_tryton',
'/sincronizar_ventas_tryton',
'/sincronizar_productos_tryton',
'/ventas_para_tryton',
'/cuadres_de_tarro',
'/compra_admin',
'/cuadrar_tarro',
]
const router = createRouter({ const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), history: createWebHistory(import.meta.env.BASE_URL),
routes: setupLayouts(routes), routes: setupLayouts(routes),
}) })
router.beforeEach((to, from, next) => {
const isAuthenticated = !!localStorage.getItem('access_token')
const requiresAuth = to.meta.requiresAuth === true
const requiresAdmin = to.meta.requiresAdmin === true || ADMIN_ROUTES.includes(to.path)
const authStore = useAuthStore()
if (requiresAuth && !isAuthenticated) {
next({ path: '/autenticarse', replace: true })
} else if (requiresAdmin && !authStore.isAdmin) {
next({ path: '/', replace: true })
} else {
next()
}
})
// Workaround for https://github.com/vitejs/vite/issues/11804 // Workaround for https://github.com/vitejs/vite/issues/11804
router.onError((err, to) => { router.onError((err, to) => {
if (err?.message?.includes?.('Failed to fetch dynamically imported module')) { if (err?.message?.includes?.('Failed to fetch dynamically imported module')) {

View File

@@ -39,6 +39,9 @@ export default defineConfig({
imports: [ imports: [
'vue', 'vue',
'vue-router', 'vue-router',
{
'unplugin-vue-router': ['definePage'],
},
], ],
eslintrc: { eslintrc: {
enabled: true, enabled: true,