feat: add auth protection to routes using meta fields
This commit is contained in:
@@ -3,5 +3,9 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
//
|
definePage({
|
||||||
|
meta: {
|
||||||
|
requiresAuth: true
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -3,5 +3,9 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
//
|
definePage({
|
||||||
|
meta: {
|
||||||
|
requiresAuth: true
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -15,6 +15,17 @@ const router = createRouter({
|
|||||||
routes: setupLayouts(routes),
|
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
|
// 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')) {
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user