48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
import { useUserStore } from '../stores/userStore'
|
|
|
|
const BASE_URL = (import.meta as unknown as { env: { BASE_URL?: string } }).env?.BASE_URL || '/'
|
|
const router = createRouter({
|
|
history: createWebHistory(BASE_URL),
|
|
routes: [
|
|
{
|
|
path: '/',
|
|
name: 'home',
|
|
component: () => import('../views/HomeView.vue'),
|
|
},
|
|
{
|
|
path: '/about',
|
|
name: 'about',
|
|
component: () => import('../views/AboutView.vue'),
|
|
},
|
|
{
|
|
path: '/login',
|
|
name: 'login',
|
|
component: () => import('../views/LoginView.vue'),
|
|
meta: { guestOnly: true },
|
|
},
|
|
{
|
|
path: '/register',
|
|
name: 'register',
|
|
component: () => import('../views/RegisterView.vue'),
|
|
meta: { guestOnly: true },
|
|
},
|
|
],
|
|
})
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
const userStore = useUserStore()
|
|
const isAuthenticated = userStore.isAuthenticated
|
|
// const is_manager = userStore.user?.is_manager || false
|
|
|
|
if (to.meta?.guestOnly && isAuthenticated) {
|
|
return next({ path: '/' })
|
|
}
|
|
if (to.meta?.requiresAuth && !isAuthenticated) {
|
|
return next({ path: '/login', query: { redirect: to.fullPath } })
|
|
}
|
|
|
|
return next()
|
|
})
|
|
|
|
export default router
|