feat: add auth protection to routes using meta fields
This commit is contained in:
@@ -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')) {
|
||||
|
||||
Reference in New Issue
Block a user