From 445d95057a7395dacbea495dd0daa0810df11bcd Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 15 Mar 2024 12:42:08 +0100 Subject: [PATCH] refs #6930 feat: trycatchfinally logout --- src/composables/useSession.js | 69 ++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/src/composables/useSession.js b/src/composables/useSession.js index 580efbdf8..507e63c0f 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -2,9 +2,11 @@ import { useState } from './useState'; import { useRole } from './useRole'; import { useUserConfig } from './useUserConfig'; import axios from 'axios'; - +import useNotify from './useNotify'; export function useSession() { + const { notify } = useNotify(); + function getToken() { const localToken = localStorage.getItem('token'); const sessionToken = sessionStorage.getItem('token'); @@ -29,37 +31,52 @@ export function useSession() { } async function destroy() { - if (localStorage.getItem('tokenMultimedia')){ - await axios.post('VnUsers/logoutMultimedia', null, { - headers: {Authorization: localStorage.getItem('tokenMultimedia') } - }); - localStorage.removeItem('tokenMultimedia') - + if (localStorage.getItem('tokenMultimedia')) { + try { + await axios.post('Accounts/logout', null, { + headers: { Authorization: localStorage.getItem('tokenMultimedia') }, + }); + } catch (error) { + notify('errors.userConfig', 'negative'); + } finally { + localStorage.removeItem('tokenMultimedia'); + } } - if (localStorage.getItem('token')){ - await axios.post('VnUsers/logout', null, { - headers: {Authorization: localStorage.getItem('token') } - }); - localStorage.removeItem('token') + if (localStorage.getItem('token')) { + try { + await axios.post('VnUsers/logout', null, { + headers: { Authorization: localStorage.getItem('token') }, + }); + } catch (error) { + notify('errors.userConfig', 'negative'); + } finally { + localStorage.removeItem('token'); + } } - - if (sessionStorage.getItem('tokenMultimedia')){ - await axios.post('VnUsers/logoutMultimedia', null, { - headers: {Authorization: sessionStorage.getItem('tokenMultimedia') } - }); - sessionStorage.removeItem('tokenMultimedia') - + if (sessionStorage.getItem('tokenMultimedia')) { + try { + await axios.post('Accounts/logout', null, { + headers: { Authorization: sessionStorage.getItem('tokenMultimedia') }, + }); + } catch (error) { + notify('errors.userConfig', 'negative'); + } finally { + sessionStorage.removeItem('tokenMultimedia'); + } } - if (sessionStorage.getItem('token')){ - await axios.post('VnUsers/logout', null, { - headers: {Authorization: sessionStorage.getItem('token') } - }); - sessionStorage.removeItem('token') + if (sessionStorage.getItem('token')) { + try { + await axios.post('VnUsers/logout', null, { + headers: { Authorization: sessionStorage.getItem('token') }, + }); + } catch (error) { + notify('errors.userConfig', 'negative'); + } finally { + sessionStorage.removeItem('token'); + } } - - const { setUser } = useState(); setUser({