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'
import RecipeDetail from '@/views/RecipeDetail.vue'
import UserDetail from '@/views/UserDetail.vue'
import UserManagement from '@/views/UserManagement.vue'
import { useAuthStore } from '@/stores/authStore'
const routes = [
{
......@@ -26,32 +27,38 @@ const routes = [
{
path: '/recipes/search',
name: 'AISearch',
component: AISearch
component: AISearch,
meta: { requiresAuth: true }
},
{
path: '/recipes',
name: 'Recipes',
component: Recipes
component: Recipes,
meta: { requiresAuth: true }
},
{
path: '/recipes/create',
name: 'CreateRecipe',
component: CreateRecipe
component: CreateRecipe,
meta: { requiresAuth: true }
},
{
path: '/recipes/detail',
name: 'RecipeDetail',
component: RecipeDetail
component: RecipeDetail,
meta: { requiresAuth: true }
},
{
path: '/users/detail',
name: 'UserDetail',
component: UserDetail
component: UserDetail,
meta: { requiresAuth: true }
},
{
path: '/users/management',
name: 'UserManagement',
component: UserManagement
component: UserManagement,
meta: { requiresAuth: true }
}
]
......@@ -60,4 +67,18 @@ const router = createRouter({
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
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