feat(index.js): añadidas restricciones de acceso para usuarios no autenticados

parent 8c0af869
Showing with 27 additions and 6 deletions
...@@ -7,6 +7,7 @@ import CreateRecipe from '@/views/CreateRecipe.vue' ...@@ -7,6 +7,7 @@ import CreateRecipe from '@/views/CreateRecipe.vue'
import RecipeDetail from '@/views/RecipeDetail.vue' import RecipeDetail from '@/views/RecipeDetail.vue'
import UserDetail from '@/views/UserDetail.vue' import UserDetail from '@/views/UserDetail.vue'
import UserManagement from '@/views/UserManagement.vue' import UserManagement from '@/views/UserManagement.vue'
import { useAuthStore } from '@/stores/authStore'
const routes = [ const routes = [
{ {
...@@ -26,32 +27,38 @@ const routes = [ ...@@ -26,32 +27,38 @@ const routes = [
{ {
path: '/recipes/search', path: '/recipes/search',
name: 'AISearch', name: 'AISearch',
component: AISearch component: AISearch,
meta: { requiresAuth: true }
}, },
{ {
path: '/recipes', path: '/recipes',
name: 'Recipes', name: 'Recipes',
component: Recipes component: Recipes,
meta: { requiresAuth: true }
}, },
{ {
path: '/recipes/create', path: '/recipes/create',
name: 'CreateRecipe', name: 'CreateRecipe',
component: CreateRecipe component: CreateRecipe,
meta: { requiresAuth: true }
}, },
{ {
path: '/recipes/detail', path: '/recipes/detail',
name: 'RecipeDetail', name: 'RecipeDetail',
component: RecipeDetail component: RecipeDetail,
meta: { requiresAuth: true }
}, },
{ {
path: '/users/detail', path: '/users/detail',
name: 'UserDetail', name: 'UserDetail',
component: UserDetail component: UserDetail,
meta: { requiresAuth: true }
}, },
{ {
path: '/users/management', path: '/users/management',
name: 'UserManagement', name: 'UserManagement',
component: UserManagement component: UserManagement,
meta: { requiresAuth: true }
} }
] ]
...@@ -60,4 +67,18 @@ const router = createRouter({ ...@@ -60,4 +67,18 @@ const router = createRouter({
routes, routes,
}) })
router.beforeEach((to, from, next) => {
const authStore = useAuthStore();
const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
// Si el usuario no está autenticado
if (requiresAuth && !authStore.isAuthenticated) {
next({ path: '/auth/login' });
} else {
next();
}
// Si el usuario no es ADMIN ...
})
export default router export default router
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment