45 lines
1.0 KiB
TypeScript
45 lines
1.0 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router'
|
|||
|
|
import { useAuthStore } from '../stores/auth'
|
||
|
|
|
||
|
|
const router = createRouter({
|
||
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
||
|
|
routes: [
|
||
|
|
{
|
||
|
|
path: '/',
|
||
|
|
name: 'home',
|
||
|
|
component: () => import('../views/HomeView.vue'),
|
||
|
|
meta: { requiresAuth: true },
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/login',
|
||
|
|
name: 'login',
|
||
|
|
component: () => import('../views/LoginView.vue'),
|
||
|
|
meta: { guestOnly: true },
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/register',
|
||
|
|
name: 'register',
|
||
|
|
component: () => import('../views/RegisterView.vue'),
|
||
|
|
meta: { guestOnly: true },
|
||
|
|
},
|
||
|
|
{ path: '/:pathMatch(.*)*', redirect: { name: 'home' } },
|
||
|
|
],
|
||
|
|
})
|
||
|
|
|
||
|
|
router.beforeEach((to) => {
|
||
|
|
const auth = useAuthStore()
|
||
|
|
|
||
|
|
if (to.meta.requiresAuth && !auth.isAuthenticated) {
|
||
|
|
return {
|
||
|
|
name: 'login',
|
||
|
|
query: to.fullPath === '/' ? {} : { redirect: to.fullPath },
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (to.meta.guestOnly && auth.isAuthenticated) {
|
||
|
|
return { name: 'home' }
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
export default router
|