From 63d195cbbf0de82a9bc1236bc0671b5dd296e914 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 15 Mar 2024 13:21:26 +0100 Subject: [PATCH] refs #6930 perf: refactor destroy method --- src/composables/useSession.js | 65 +++++++++++------------------------ 1 file changed, 20 insertions(+), 45 deletions(-) diff --git a/src/composables/useSession.js b/src/composables/useSession.js index aa20dde20..45d439b46 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -29,52 +29,27 @@ export function useSession() { sessionStorage.setItem('tokenMultimedia', data.tokenMultimedia); } } - + async function destroyToken(url, storage, key) { + if (storage.getItem(key)) { + try { + await axios.post(url, null, { + headers: { Authorization: storage.getItem(key) }, + }); + } catch (error) { + notify('errors.statusUnauthorized', 'negative'); + } finally { + storage.removeItem(key); + } + } + } async function destroy() { - if (localStorage.getItem('tokenMultimedia')) { - try { - await axios.post('Accounts/logout', null, { - headers: { Authorization: localStorage.getItem('tokenMultimedia') }, - }); - } catch (error) { - notify('errors.statusUnauthorized', 'negative'); - } finally { - localStorage.removeItem('tokenMultimedia'); - } - } - if (localStorage.getItem('token')) { - try { - await axios.post('VnUsers/logout', null, { - headers: { Authorization: localStorage.getItem('token') }, - }); - } catch (error) { - notify('errors.statusUnauthorized', 'negative'); - } finally { - localStorage.removeItem('token'); - } - } - - if (sessionStorage.getItem('tokenMultimedia')) { - try { - await axios.post('Accounts/logout', null, { - headers: { Authorization: sessionStorage.getItem('tokenMultimedia') }, - }); - } catch (error) { - notify('errors.statusUnauthorized', 'negative'); - } finally { - sessionStorage.removeItem('tokenMultimedia'); - } - } - if (sessionStorage.getItem('token')) { - try { - await axios.post('VnUsers/logout', null, { - headers: { Authorization: sessionStorage.getItem('token') }, - }); - } catch (error) { - notify('errors.statusUnauthorized', 'negative'); - } finally { - sessionStorage.removeItem('token'); - } + const tokens = { + tokenMultimedia: 'Accounts/logout', + token: 'VnUsers/logout', + }; + for (const [key, url] of Object.entries(tokens)) { + await destroyToken(url, localStorage, key); + await destroyToken(url, sessionStorage, key); } const { setUser } = useState();