primaryRoutes.js fetch() fix and refactoring
This commit is contained in:
@@ -7,95 +7,40 @@ import { wrap } from 'svelte-spa-router/wrap'
|
|||||||
import Game from '../pages/game/Game.svelte';
|
import Game from '../pages/game/Game.svelte';
|
||||||
import Ranking from '../pages/game/Ranking.svelte';
|
import Ranking from '../pages/game/Ranking.svelte';
|
||||||
import GameSpectator from '../pages/game/GameSpectator.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 = {
|
export const primaryRoutes = {
|
||||||
'/': SplashPage,
|
'/': SplashPage,
|
||||||
'/2fa': TwoFactorAuthentication,
|
'/2fa': TwoFactorAuthentication,
|
||||||
'/game': wrap({
|
'/game': wrap({
|
||||||
component: Game,
|
component: Game,
|
||||||
conditions: [
|
conditions: [checkLogin]
|
||||||
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({
|
'/spectator': wrap({
|
||||||
component: GameSpectator,
|
component: GameSpectator,
|
||||||
conditions: [
|
conditions: [checkLogin]
|
||||||
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({
|
'/ranking': wrap({
|
||||||
component: Ranking,
|
component: Ranking,
|
||||||
conditions: [
|
conditions: [checkLogin]
|
||||||
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({
|
'/profile': wrap({
|
||||||
component: ProfilePage,
|
component: ProfilePage,
|
||||||
conditions: [
|
conditions: [checkLogin]
|
||||||
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({
|
'/profile/*': wrap({
|
||||||
component: ProfilePage,
|
component: ProfilePage,
|
||||||
conditions: [
|
conditions: [checkLogin]
|
||||||
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,
|
'/unauthorized-access': UnauthorizedAccessPage,
|
||||||
'*': NotFound
|
'*': NotFound
|
||||||
|
|||||||
Reference in New Issue
Block a user