From 825e4497d288aaaa7253edbe9f0196298a3407ac Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 3 Jul 2024 14:18:24 +0200 Subject: [PATCH] feat: handle request when unauthorized --- src/composables/useSession.js | 42 ++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/composables/useSession.js b/src/composables/useSession.js index ca2abef00..10791c9c8 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -58,31 +58,37 @@ export function useSession() { } } } - async function destroy() { + async function destroy(destroyTokens = true) { const tokens = { tokenMultimedia: 'Accounts/logout', token: 'VnUsers/logout', }; const storage = keepLogin() ? localStorage : sessionStorage; + let destroyTokenPromises = []; + try { + if (destroyTokens) { + const { data: isValidToken } = await axios.get('VnUsers/validateToken'); + if (isValidToken) + destroyTokenPromises = Object.entries(tokens).map(([key, url]) => + destroyToken(url, storage, key) + ); + } + } finally { + localStorage.clear(); + sessionStorage.clear(); + await Promise.allSettled(destroyTokenPromises); + const { setUser } = useState(); - for (const [key, url] of Object.entries(tokens)) { - await destroyToken(url, storage, key); + setUser({ + id: 0, + name: '', + nickname: '', + lang: '', + darkMode: null, + }); + + stopRenewer(); } - - localStorage.clear(); - sessionStorage.clear(); - - const { setUser } = useState(); - - setUser({ - id: 0, - name: '', - nickname: '', - lang: '', - darkMode: null, - }); - - stopRenewer(); } async function login(data) {