import {createRouter, createWebHistory} from 'vue-router' import HomeView from '../views/HomeView.vue' import {useAuthStore} from '@/stores/auth'; const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'home', component: HomeView, }, { path: '', name: 'member', component: () => import('../views/Member.vue'), children: [ { path: '', name: 'dashboard', component: () => import('../components/TheWelcome.vue'), meta: {requiresAuth: true} }, { path: '/upload', name: 'upload', component: () => import('../views/games/UploadView.vue'), meta: {requiresAuth: true} }, { path: '/events', name: 'events', component: () => import('../views/mqtt/Events.vue'), meta: {requiresAuth: true} }, { path: '/games/:gameId', name: 'game', component: () => import('../views/games/GameView.vue'), meta: {requiresAuth: true,}, }, { path: '/games', name: 'games', component: () => import('../views/games/GamesView.vue'), meta: {requiresAuth: true,} }, { path: '/games', name: 'games', component: () => import('../views/games/GamesView.vue'), meta: {requiresAuth: true,} }, { path: '/login', name: 'login', component: () => import('../views/auths/Login.vue'), meta: {requiresAuth: false,} }, { path: '/sign-up', name: 'sign-up', component: () => import('../views/auths/SignUp.vue'), meta: {requiresAuth: false,} }, ], }, { path: '', name: 'limited', component: () => import('../views/Limited.vue'), children: [ { path: '/close', name: 'close', component: () => import('../views/players/Close.vue') }, { path: '/action', name: 'action', component: () => import('../views/players/QrAction.vue'), }, { path: '/:catchAll(.*)', name: 'shit', component: () => import('../views/NotFound.vue'), }, ] } ] }) router.beforeEach((to, from, next) => { if (to.meta.requiresAuth === undefined) next(); if (useAuthStore().isAuth() === to.meta.requiresAuth) { // If the condition is met, allow access to the route next(); } else if (to.meta.requiresAuth) { console.error('sneaky') // If the condition is not met, redirect to another route next('/login'); // Redirect to the login page } else { console.error('sneaky') next('/') } }) export default router