diff --git a/src/App.vue b/src/App.vue
index 6a287e6..8ce5497 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -1,8 +1,16 @@
diff --git a/src/composables/useIngame.ts b/src/composables/useIngame.ts
index d843415..5c76775 100644
--- a/src/composables/useIngame.ts
+++ b/src/composables/useIngame.ts
@@ -9,12 +9,17 @@ const api = window?.__RUNTIME_CONFIG__?.API_URL;
export const useIngame = defineStore('ingame', () => {
const state: Ref<'Lobby' | 'InGame' | 'SelectTrump' | 'TieBreak' | 'FinishedMatch' | null> = ref(null);
const data: Ref = ref(null);
+ const isWsConnected: Ref = ref(false);
function setIngame(newState: 'Lobby' | 'InGame' | 'SelectTrump' | 'TieBreak' | 'FinishedMatch', newData: GameInfo | LobbyInfo | TieInfo | TrumpInfo | WonInfo) {
state.value = newState;
data.value = newData;
}
+ function setWsConnected(connected: boolean) {
+ isWsConnected.value = connected;
+ }
+
async function requestGame(gameId: string) {
await axios.get(`${api}/status/${gameId}`, {withCredentials: true}).then((response) => {
setIngame(response.data.state, response.data.data);
@@ -24,7 +29,8 @@ export const useIngame = defineStore('ingame', () => {
function clearIngame() {
state.value = null;
data.value = null;
+ isWsConnected.value = false;
}
- return { state, data, requestGame, setIngame, clearIngame };
+ return { state, data, isWsConnected, requestGame, setIngame, clearIngame, setWsConnected };
});
diff --git a/src/composables/useUserInfo.ts b/src/composables/useUserInfo.ts
index 77418df..1a5ec26 100644
--- a/src/composables/useUserInfo.ts
+++ b/src/composables/useUserInfo.ts
@@ -21,10 +21,19 @@ export const useUserInfo = defineStore('userInfo', () => {
async function requestState() {
await axios.get(`${api}/status`, {withCredentials: true}).then((response) => {
console.log("STATUS DATA:" + response.data.status + response.data.inGame)
- username.value = response.data.username;
- if (response.data.gameId) {
- console.log("GAMEID:" + response.data.gameId)
- gameId.value = response.data.gameId;
+ if (response.data.status === 'authenticated') {
+ username.value = response.data.username;
+ userId.value = response.data.userId;
+ if (response.data.gameId) {
+ console.log("GAMEID:" + response.data.gameId)
+ gameId.value = response.data.gameId;
+ } else {
+ gameId.value = null;
+ }
+ } else {
+ username.value = null;
+ userId.value = null;
+ gameId.value = null;
}
})
}
diff --git a/src/router/index.ts b/src/router/index.ts
index 2118989..98e5149 100644
--- a/src/router/index.ts
+++ b/src/router/index.ts
@@ -73,8 +73,12 @@ router.beforeEach(async (to, from, next) => {
if (isOnline) {
try {
- const response = await axios.get(`${api}/userInfo`, { withCredentials: true });
- info.setUserInfo(response.data.username, response.data.userId);
+ await info.requestState();
+
+ if (!info.username) {
+ info.clearUserInfo();
+ return next('/login');
+ }
return next({ name: 'mainmenu' });
@@ -92,11 +96,17 @@ router.beforeEach(async (to, from, next) => {
if (!to.meta.requiresAuth) return next();
try {
- await axios.get(`${api}/userInfo`, { withCredentials: true }).then(
- res => {
- info.setUserInfo(res.data.username, res.data.userId);
- }
- );
+ await info.requestState();
+
+ if (!info.username) {
+ info.clearUserInfo();
+ return next('/login');
+ }
+
+ if (info.gameId && to.name !== 'game') {
+ return next({ name: 'game' });
+ }
+
next();
} catch (err) {
info.clearUserInfo();
diff --git a/src/services/ws.ts b/src/services/ws.ts
index 97bacc6..56d820b 100644
--- a/src/services/ws.ts
+++ b/src/services/ws.ts
@@ -160,14 +160,15 @@ function setupSocketHandlers(socket: WebSocket) {
stopHeartbeat();
failAllPending("WebSocket closed");
+ if (uState) {
+ uState.setWsConnected(false);
+ }
+
if (ev.wasClean) {
console.log(`[WS] Closed cleanly: code=${ev.code} reason=${ev.reason}`);
} else {
console.warn("[WS] Connection died");
}
-
- // You redirect here — if you don’t want auto reconnect, keep as is.
- router.replace("/");
};
}
@@ -191,6 +192,9 @@ export function connectWebSocket(url?: string): Promise {
const prevOnError = ws!.onerror;
ws!.onopen = (ev) => {
if (prevOnOpen) prevOnOpen.call(ws!, ev);
+ if (uState) {
+ uState.setWsConnected(true);
+ }
resolve();
};
ws!.onerror = (err) => {
@@ -207,6 +211,9 @@ export function connectWebSocket(url?: string): Promise {
return new Promise((resolve, reject) => {
ws!.onopen = () => {
console.log("[WS] Connected");
+ if (uState) {
+ uState.setWsConnected(true);
+ }
startHeartbeat();
resolve();
};
@@ -224,6 +231,9 @@ export function disconnectWebSocket(code = 1000, reason = "Client disconnect") {
ws.close(code, reason);
} catch {}
ws = null;
+ if (uState) {
+ uState.setWsConnected(false);
+ }
}
}
diff --git a/src/views/CreateGame.vue b/src/views/CreateGame.vue
index c9a3b0d..aa30db1 100644
--- a/src/views/CreateGame.vue
+++ b/src/views/CreateGame.vue
@@ -3,6 +3,7 @@ import { ref } from 'vue';
import { useQuasar } from 'quasar';
import { useRouter } from 'vue-router';
import axios from "axios";
+import {useUserInfo} from "@/composables/useUserInfo";
const api = window?.__RUNTIME_CONFIG__?.API_URL;
const lobbyName = ref('');
@@ -11,13 +12,14 @@ const playerAmount = ref(2);
const $q = useQuasar();
const isLoading = ref(false);
const router = useRouter();
+const info = useUserInfo();
const createGameQuasar = async () => {
if (!lobbyName.value) {
$q.notify({ message: 'Lobby-Name wird benötigt', color: 'red', position: 'top', icon: 'cancel' });
return;
}
isLoading.value = true;
- axios.post(`${api}/createGame`, {lobbyname: lobbyName.value, playeramount: playerAmount.value.toString()}, {withCredentials: true}).then((response) => {
+ axios.post(`${api}/createGame`, {lobbyname: lobbyName.value, playeramount: playerAmount.value.toString()}, {withCredentials: true}).then(async (response) => {
const responseData = response.data
console.log("Response" + responseData.status)
$q.notify({
@@ -26,12 +28,12 @@ const createGameQuasar = async () => {
icon: 'check_circle',
position: 'top'
});
+ await info.requestState();
router.replace("/game")
}).catch((err) => {
console.log("ERROR:" + err)
- }).finally(() =>
isLoading.value = false
- )
+ })
}
diff --git a/src/views/Game.vue b/src/views/Game.vue
index c837e41..757562f 100644
--- a/src/views/Game.vue
+++ b/src/views/Game.vue
@@ -7,6 +7,7 @@ import LobbyComponent from "@/components/lobby/LobbyComponent.vue";
import {storeToRefs} from "pinia";
import {useQuasar} from "quasar";
import router from "@/router";
+import {ref, watch} from "vue";
const wb = useWebSocket()
const ig = useIngame()
@@ -20,7 +21,6 @@ ui.requestState().then(() => {
$q.notify({
message: "You're not in any game!",
color: "negative"
-
})
router.replace("/")
} else {
diff --git a/src/views/JoinGameView.vue b/src/views/JoinGameView.vue
index 021f550..17a4504 100644
--- a/src/views/JoinGameView.vue
+++ b/src/views/JoinGameView.vue
@@ -1,13 +1,24 @@