diff --git a/RollCallClient/src/router/index.js b/RollCallClient/src/router/index.js new file mode 100644 index 0000000..77b3a9e --- /dev/null +++ b/RollCallClient/src/router/index.js @@ -0,0 +1,66 @@ +// router/index.js + +import { createRouter, createWebHistory } from 'vue-router' + +import StudentManageView from "@/views/Manage.vue"; +import LayoutView from '@/components/Layout.vue'; +import HomeView from '@/views/Home.vue' +import LoginView from '@/views/Login.vue' +import RollCallView from '@/views/RollCall.vue' +import { useTokenStore } from "@/store/token.js"; + + +const routes = [ + { + path: '/login', + name: 'login', + component: LoginView + }, + { + path: '/', + component: LayoutView, + children: [ + { + path: '', + redirect: '/home' + }, + { + path: '/home', + name: 'home', + component: HomeView + }, + { + path: '/rollcall', + name: 'rollcall', + component: RollCallView + }, + { + path: '/student', + name: 'student', + component: StudentManageView + } + ] + } +] + +const router = createRouter({ + history: createWebHistory(), + routes +}); + +export function setupRouterGuard(router) { + router.beforeEach((to, from, next) => { + const tokenStore = useTokenStore() + const isAuthenticated = tokenStore.token !== '' + + if (to.name !== 'login' && !isAuthenticated) { + next({ name: 'login' }) + } else if (to.name === 'login' && isAuthenticated) { + next({ name: 'home' }) + } else { + next() + } + }) +} + +export default router \ No newline at end of file