feat: FRO-2 Implement Login Component (#8)
Reviewed-on: #8 Reviewed-by: lq64 <lq@blackhole.local> Co-authored-by: Janis <janis.e.20@gmx.de> Co-committed-by: Janis <janis.e.20@gmx.de>
This commit is contained in:
@@ -1,49 +1,60 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import HomeView from '../views/HomeView.vue'
|
||||
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 axios from "axios";
|
||||
import { useUserInfo } from "@/composables/useUserInfo";
|
||||
|
||||
const api = window?.__RUNTIME_CONFIG__?.API_URL;
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
routes: [
|
||||
{
|
||||
path: '/',
|
||||
name: 'home',
|
||||
component: HomeView,
|
||||
name: 'mainmenu',
|
||||
component: MainMenuView,
|
||||
meta: { requiresAuth: true }
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: LoginView,
|
||||
},
|
||||
{
|
||||
path: '/about',
|
||||
name: 'about',
|
||||
// route level code-splitting
|
||||
// this generates a separate chunk (About.[hash].js) for this route
|
||||
// which is lazy-loaded when the route is visited.
|
||||
component: () => import('../views/AboutView.vue'),
|
||||
},
|
||||
{
|
||||
path: '/mainmenu',
|
||||
name: 'mainmenu',
|
||||
component: MainMenuView
|
||||
meta: { requiresAuth: false }
|
||||
},
|
||||
{
|
||||
path: '/create',
|
||||
name: 'create-Game',
|
||||
component: createGameView
|
||||
component: createGameView,
|
||||
meta: { requiresAuth: true }
|
||||
},
|
||||
{
|
||||
path: '/join',
|
||||
name: 'join-Game',
|
||||
component: joinGameView
|
||||
},
|
||||
|
||||
|
||||
component: joinGameView,
|
||||
meta: { requiresAuth: true }
|
||||
}
|
||||
],
|
||||
})
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user