Files
KnockOutWhist-Frontend/src/router/index.ts
lq64 adbe2be534 feat: FRO-17 Added Rule Component and changed Mainmenu structure (#11)
Added a Rule Component and changed MainMenu Structure

Co-authored-by: LQ63 <lkhermann@web.de>
Reviewed-on: #11
Reviewed-by: Janis <janis-e@gmx.de>
Co-authored-by: lq64 <lq@blackhole.local>
Co-committed-by: lq64 <lq@blackhole.local>
2025-12-10 14:12:12 +01:00

76 lines
1.8 KiB
TypeScript

import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import MainMenuView from '../views/MainMenuView.vue'
import createGameView from '../views/CreateGame.vue'
import joinGameView from "@/views/JoinGameView.vue";
import defaultMenu from "../components/DefaultMenu.vue"
import axios from "axios";
import { useUserInfo } from "@/composables/useUserInfo";
import rulesView from "../components/Rules.vue";
const api = window?.__RUNTIME_CONFIG__?.API_URL;
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/mainmenu/',
component: MainMenuView,
meta: { requiresAuth: true },
children: [
{
path: '',
name: 'mainmenu',
component: defaultMenu,
meta: { requiresAuth: true }
},
{
path: 'create',
name: 'create-Game',
component: createGameView,
meta: {requiresAuth: true }
},
{
path: 'join',
name: 'join-Game',
component: joinGameView,
meta: {requiresAuth: true }
},
{
path: 'rules',
name: 'rules-Game',
component: rulesView,
meta: {requiresAuth: true }
},
],
},
{
path: '/login',
name: 'login',
component: LoginView,
meta: { requiresAuth: false }
},
],
})
router.beforeEach(async (to, from, next) => {
const info = useUserInfo();
if (!to.meta.requiresAuth) return next();
try {
await axios.get(`${api}/userInfo`, { withCredentials: true }).then(
res => {
info.setUserInfo(res.data.username, res.data.userId);
}
);
next();
} catch (err) {
info.clearUserInfo();
next('/login');
}
});
export default router