import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' import store from '../store/index' import { getKeycloak, keycloakSetup } from '../authentication/AuthHelper' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'main', component: HomeView, meta: { requiresAuth: false } }, { path: '/contracts', name: 'contracts', // this page is lazy-loaded when the route is visited. component: () => import('../views/ContractsView.vue'), meta: { requiresAuth: true } }, { path: '/support', name: 'support', component: () => import('../views/SupportView.vue'), meta: { requiresAuth: true } }, { path: '/administration', name: 'administration', component: () => import('../views/AdministrationView.vue'), meta: { requiresAuth: true, requiresAdmin: true } }, { path: '/arbeiten', name: 'arbeiten', component: () => import('../views/ArbeitenView.vue'), meta: { requiresAuth: false } }, { path: '/contact', name: 'contact', component: () => import('../views/ContactView.vue'), meta: { requiresAuth: false } }, { path: '/about', name: 'about', component: () => import('../views/AboutView.vue'), meta: { requiresAuth: false } }, { path: '/contract', name: 'contract', component: () => import('../views/ContractView.vue'), meta: { requiresAuth: true } }, { path: '/contracts/register', name: 'contractRegistration', component: () => import('../views/OpenContractsView.vue'), meta: { requiresAuth: true } }, { path: '/forbidden', name: 'forbidden', component: () => import('../components/ForbiddenPage.vue'), meta: { requiresAuth: false } }, { path: '/error', name: 'error', component: () => import('../views/ErrorView.vue'), meta: { requiresAuth: false } }, { path: '/:pathMatch(.*)*', name: 'notfound', component: () => import('../components/PageNotFound.vue'), meta: { requiresAuth: false } } ] }) router.beforeEach(async (to, from, next) => { //if user was logged in but reloaded page. So we have all data, need only restore it if (store.state.keycloak.keycloak !== null && getKeycloak() === null) { await keycloakSetup() } if (to.meta.requiresAuth === true) { if (getKeycloak() === null) { await keycloakSetup() store.commit('initKeycloak', getKeycloak()) store.commit('isAnonym', false) } if (!getKeycloak().authenticated) { next('/forbidden') } next() } else { next() } }) export default router