feat: add auth protection to routes using meta fields

This commit is contained in:
mono
2026-03-15 23:32:36 -05:00
parent 6e3d9b8a49
commit eb4811ab8b
4 changed files with 24 additions and 2 deletions

View File

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

View File

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

View File

@@ -15,6 +15,17 @@ const router = createRouter({
routes: setupLayouts(routes),
})
router.beforeEach((to, from, next) => {
const isAuthenticated = !!localStorage.getItem('access_token')
const requiresAuth = to.meta.requiresAuth === true
if (requiresAuth && !isAuthenticated) {
next({ path: '/autenticarse', replace: true })
} else {
next()
}
})
// Workaround for https://github.com/vitejs/vite/issues/11804
router.onError((err, to) => {
if (err?.message?.includes?.('Failed to fetch dynamically imported module')) {

View File

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