59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
import HomeView from '../views/HomeView.vue'
|
|
import { useAuthStore } from '@/stores/auth'
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
routes: [
|
|
{
|
|
path: '/',
|
|
name: 'home',
|
|
component: HomeView,
|
|
},
|
|
{
|
|
path: '/about',
|
|
name: 'about',
|
|
component: () => import('../views/AboutView.vue'),
|
|
},
|
|
{
|
|
path: '/login',
|
|
name: 'login',
|
|
component: () => import('../views/LoginView.vue'),
|
|
},
|
|
{
|
|
path: '/profile',
|
|
name: 'profile',
|
|
component: () => import('../views/ProfileView.vue'),
|
|
meta: { requiresAuth: true },
|
|
},
|
|
{
|
|
path: '/admin',
|
|
name: 'admin',
|
|
component: () => import('../views/AdminView.vue'),
|
|
meta: { requiresAuth: true, adminOnly: true },
|
|
},
|
|
{
|
|
path: '/admin/users',
|
|
name: 'users',
|
|
component: () => import('../views/UserManageView.vue'),
|
|
meta: { requiresAuth: true, adminOnly: true },
|
|
},
|
|
],
|
|
})
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
const authStore = useAuthStore()
|
|
|
|
if (to.meta.requiresAuth && !authStore.isLoggedIn) {
|
|
next({ name: 'login' })
|
|
} else if (to.meta.adminOnly && authStore.user?.role !== 'admin') {
|
|
next({ name: 'profile' })
|
|
} else if (to.name === 'login' && authStore.isLoggedIn) {
|
|
next({ name: 'profile' })
|
|
} else {
|
|
next()
|
|
}
|
|
})
|
|
|
|
export default router
|