72 lines
1.4 KiB
TypeScript
72 lines
1.4 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: {
|
|
guestOnly: true,
|
|
},
|
|
},
|
|
{
|
|
path: '/login',
|
|
name: 'login',
|
|
component: () => import('@/views/LoginView.vue'),
|
|
meta: {
|
|
guestOnly: true,
|
|
},
|
|
},
|
|
{
|
|
path: '/signup',
|
|
name: 'signup',
|
|
component: () => import('@/views/SignupView.vue'),
|
|
meta: {
|
|
guestOnly: true,
|
|
},
|
|
},
|
|
{
|
|
path: '/dashboard',
|
|
name: 'dashboard',
|
|
component: () => import('@/views/DashboardView.vue'),
|
|
meta: {
|
|
requiresAuth: true,
|
|
},
|
|
},
|
|
],
|
|
})
|
|
|
|
router.beforeEach(async (to) => {
|
|
const authStore = useAuthStore()
|
|
|
|
if (to.meta.requiresAuth && !authStore.isAuthenticated) {
|
|
const restoredSession = await authStore.fetchMe()
|
|
if (!restoredSession) {
|
|
return {
|
|
name: 'login',
|
|
query: {
|
|
redirect: to.fullPath,
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
if (to.meta.guestOnly) {
|
|
let restoredSession = authStore.isAuthenticated
|
|
if (!restoredSession) {
|
|
restoredSession = await authStore.fetchMe()
|
|
}
|
|
|
|
if (restoredSession) {
|
|
return {
|
|
name: 'dashboard',
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
export default router
|