54 lines
1.2 KiB
TypeScript
54 lines
1.2 KiB
TypeScript
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
|
|
import { useAuthStore } from '@/stores/auth'
|
|
|
|
const routes: RouteRecordRaw[] = [
|
|
{
|
|
path: '/login',
|
|
name: 'login',
|
|
component: () => import('@/views/LoginView.vue'),
|
|
meta: { layout: 'blank', public: true },
|
|
},
|
|
{ path: '/', redirect: '/scores' },
|
|
{
|
|
path: '/scores',
|
|
name: 'scores',
|
|
component: () => import('@/views/ScoresView.vue'),
|
|
},
|
|
{
|
|
path: '/chart',
|
|
name: 'chart',
|
|
component: () => import('@/views/ChartView.vue'),
|
|
},
|
|
{
|
|
path: '/run',
|
|
name: 'run',
|
|
component: () => import('@/views/RunView.vue'),
|
|
},
|
|
{
|
|
path: '/admin/users',
|
|
name: 'admin-users',
|
|
component: () => import('@/views/AdminUsersView.vue'),
|
|
meta: { adminOnly: true },
|
|
},
|
|
{ path: '/:pathMatch(.*)*', redirect: '/scores' },
|
|
]
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
routes,
|
|
})
|
|
|
|
router.beforeEach((to) => {
|
|
const auth = useAuthStore()
|
|
if (to.meta.public) return true
|
|
if (!auth.token) {
|
|
return { path: '/login', query: { redirect: to.fullPath } }
|
|
}
|
|
if (to.meta.adminOnly && !auth.isAdmin) {
|
|
return { path: '/scores' }
|
|
}
|
|
return true
|
|
})
|
|
|
|
export default router
|