From dfd34c590b6d0957cd3da9ec2d2ece2ce8e0e0d1 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Mon, 16 Jan 2023 18:04:30 +0100 Subject: [PATCH] primaryRoutes.js fetch() fix and refactoring --- .../api_front/src/routes/primaryRoutes.js | 123 +++++------------- 1 file changed, 34 insertions(+), 89 deletions(-) diff --git a/srcs/requirements/svelte/api_front/src/routes/primaryRoutes.js b/srcs/requirements/svelte/api_front/src/routes/primaryRoutes.js index b0f64d8e..9bd7b912 100644 --- a/srcs/requirements/svelte/api_front/src/routes/primaryRoutes.js +++ b/srcs/requirements/svelte/api_front/src/routes/primaryRoutes.js @@ -7,96 +7,41 @@ import { wrap } from 'svelte-spa-router/wrap' import Game from '../pages/game/Game.svelte'; import Ranking from '../pages/game/Ranking.svelte'; import GameSpectator from '../pages/game/GameSpectator.svelte'; +import { fetchUser } from "../pieces/utils"; +async function checkLogin(detail) { + const user = await fetchUser(); + if (!user || !user.username) { + return false; + } + else { + return true; + } +} export const primaryRoutes = { - '/': SplashPage, - '/2fa': TwoFactorAuthentication, - '/game': wrap({ - component: Game, - conditions: [ - async(detail) => { - const user = await fetch('http://' + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + '/api/v2/user') - .then((resp) => resp.json()) - - console.log('in /profile what is in user') - console.log(user) - - if (user && user.username) - return true; - else - return false; - } - ] - }), - '/spectator': wrap({ - component: GameSpectator, - conditions: [ - async(detail) => { - const user = await fetch('http://' + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + '/api/v2/user') - .then((resp) => resp.json()) - - console.log('in /profile what is in user') - console.log(user) - - if (user && user.username) - return true; - else - return false; - } - ] - }), - '/ranking': wrap({ - component: Ranking, - conditions: [ - async(detail) => { - const user = await fetch('http://' + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + '/api/v2/user') - .then((resp) => resp.json()) - - console.log('in /profile what is in user') - console.log(user) - - if (user && user.username) - return true; - else - return false; - } - ] - }), - '/profile': wrap({ - component: ProfilePage, - conditions: [ - async(detail) => { - const user = await fetch('http://' + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + '/api/v2/user') - .then((resp) => resp.json()) - - console.log('in /profile what is in user') - console.log(user) - - if (user && user.username) - return true; - else - return false; - } - ] - }), - '/profile/*': wrap({ - component: ProfilePage, - conditions: [ - async(detail) => { - const user = await fetch('http://' + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + '/api/v2/user') - .then((resp) => resp.json()) - - console.log('in /profile/* what is in user') - console.log(user) - - if (user && user.username) - return true; - else - return false; - } - ] - }), - '/unauthorized-access': UnauthorizedAccessPage, - '*': NotFound + '/': SplashPage, + '/2fa': TwoFactorAuthentication, + '/game': wrap({ + component: Game, + conditions: [checkLogin] + }), + '/spectator': wrap({ + component: GameSpectator, + conditions: [checkLogin] + }), + '/ranking': wrap({ + component: Ranking, + conditions: [checkLogin] + }), + '/profile': wrap({ + component: ProfilePage, + conditions: [checkLogin] + }), + '/profile/*': wrap({ + component: ProfilePage, + conditions: [checkLogin] + }), + '/unauthorized-access': UnauthorizedAccessPage, + '*': NotFound };