From b57dc964c7e44cf99a81082fe4077bc264029340 Mon Sep 17 00:00:00 2001 From: synan798 Date: Mon, 23 Dec 2024 15:31:19 +0100 Subject: [PATCH] added checkToken --- pages/index.vue | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/pages/index.vue b/pages/index.vue index c347e1a..b8e572f 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -21,8 +21,11 @@ onMounted(async () => { setLevelbar(70); await fetchUser(); await fetchGames(); + checkToken(); + intervalId.value = await setInterval(() => { - fetchGames() + fetchGames(); + checkToken(); }, 15000); // 15 seconds }); @@ -30,6 +33,25 @@ onBeforeUnmount(() => { clearInterval(intervalId.value); }); +async function checkToken() { + try { + const res = await fetch(`https://api.spotify.com/v1/me`, { + method: 'GET', + headers: { + 'Authorization': `Bearer ${useCookie('sb-provider-token').value}`, + }, + }); + + if (!res.ok) { + console.error(`Error: ${res.status} - ${res.statusText}`); + navigateTo('/login'); + } + } catch (error) { + console.error('Failed to validate token:', error); + navigateTo('/login'); + } +} + function setLevelbar(newValue: number) { const levelbar = document.getElementById('levelbar') if (levelbar) {