Wip : Lobby

This commit is contained in:
2024-06-13 10:00:29 -04:00
parent 8a3e2babc9
commit 4a56f3ed62
4 changed files with 28 additions and 11 deletions
+3
View File
@@ -3,9 +3,12 @@
const router = useRouter();
const authStore = useAuthStore()
await authStore.whoami()
useHead
({
title
+9 -5
View File
@@ -25,16 +25,19 @@ export async function getLobby() {
export async function whoami() {
const config = useRuntimeConfig();
const req = useFetch<User>(new URL("/whoami", config.public.endpoint).href, {
const req = await useFetch<User>(
new URL("/whoami", config.public.endpoint).href,
{
credentials: "include",
});
}
);
if (
req.error.value?.statusCode &&
[401].includes(req.error.value.statusCode)
) {
await navigateTo(config.public.discordLoginEndpoint, {
external: true,
});
// await navigateTo(config.public.discordLoginEndpoint, {
// external: true,
// });
throw new Error("You are not logged in");
}
if (req.error.value) {
@@ -42,6 +45,7 @@ export async function whoami() {
"Something went wrong while trying to get user information"
);
}
return req.data;
}
+8 -2
View File
@@ -9,12 +9,17 @@ const config = useRuntimeConfig()
const route = useRoute()
const gameId = route.params.id
const { data: game, pending, error, refresh } = await useFetch<Game>(
const { data: game, pending, error, refresh } = (await useFetch<Game>(
new URL(`/games/${gameId}`, config.public.endpoint).href,
{
credentials: "include",
}
);
))
// as { data: Ref<Game>, pending: Ref<Boolean>, error: Ref<Error>, refresh: (opts?: unknown | undefined) => Promise<void> }
if (game.value == null) {
throw new Error()
}
const eventSource = subscribe(`/games/${gameId}/subscribe`)
@@ -24,6 +29,7 @@ eventSource.addEventListener("message", (message) => {
const authStore = useAuthStore()
const isSpectator = computed(() => authStore.logged && game.value.teams.every(t => t.players.every(p => p.user.userId !== authStore.selfUser.userId)))
// const current_players = computed<Array<Player>>(() => Array.from(goStore.current_game?.players ?? []).filter(p => p.team === goStore.current_game?.current_turn))
+6 -2
View File
@@ -8,9 +8,13 @@ export const useAuthStore = defineStore("auth", {
}),
actions: {
async whoami() {
const data = await whoami();
Object.assign(this.selfUser, data.value);
try {
const data = await whoami().then((data) => {
this.logged = true;
return data;
});
Object.assign(this.selfUser, data.value);
} catch (e) {}
},
oauth2_register_discord: () => {
throw Error("not implemented");