From b669adab5a52a49c406756ef696460f693e377c2 Mon Sep 17 00:00:00 2001 From: wbuezas Date: Mon, 7 Oct 2024 09:14:59 -0300 Subject: [PATCH] Add multimedia token feature --- src/stores/user.js | 59 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/src/stores/user.js b/src/stores/user.js index 0a456212..e5e2483e 100644 --- a/src/stores/user.js +++ b/src/stores/user.js @@ -7,9 +7,12 @@ import { useAppStore } from 'src/stores/app.js'; const { t } = i18n.global; const { notify } = useNotify(); +const TOKEN_MULTIMEDIA = 'tokenMultimedia'; +const TOKEN = 'token'; export const useUserStore = defineStore('user', () => { const token = ref(''); + const tokenMultimedia = ref(''); const isGuest = ref(false); const user = ref(null); // Usuario en uso => supplantedUser || mainUser const supplantedUser = ref(null); // Usuario suplantado @@ -54,15 +57,24 @@ export const useUserStore = defineStore('user', () => { const getToken = () => { const tokenValue = - sessionStorage.getItem('vnToken') || - localStorage.getItem('vnToken'); + sessionStorage.getItem(TOKEN) || localStorage.getItem(TOKEN); token.value = tokenValue; return tokenValue; }; - const setToken = _token => { - storage.value.setItem('vnToken', _token); + const getTokenMultimedia = () => { + const tokenMultimediaValue = + sessionStorage.getItem(TOKEN_MULTIMEDIA) || + localStorage.getItem(TOKEN_MULTIMEDIA); + tokenMultimedia.value = tokenMultimediaValue; + return tokenMultimediaValue; + }; + + const setToken = ({ _token, _tokenMultimedia }) => { + storage.value.setItem(TOKEN, _token); + storage.value.setItem(TOKEN_MULTIMEDIA, _tokenMultimedia); token.value = _token; + tokenMultimedia.value = _tokenMultimedia; }; const destroyToken = async (url, storageType, key) => { @@ -106,18 +118,39 @@ export const useUserStore = defineStore('user', () => { }; const setSession = data => { + setToken({ + _token: data.token, + _tokenMultimedia: data.tokenMultimedia + }); + storage.value.setItem('hederaLastUser', data.username); storage.value.setItem('created', data.created); storage.value.setItem('ttl', data.ttl); localStorage.setItem('keepLogin', keepLogin.value); }; + const fetchMultimediaToken = async data => { + const { + data: { multimediaToken } + } = await api.get('VnUsers/ShareToken', { + headers: { Authorization: data.token } + }); + return multimediaToken; + }; + const login = async (username, password, remember) => { const params = { user: username, password }; const { data } = await api.post('Accounts/login', params); + + const multimediaToken = await fetchMultimediaToken(data); + if (!multimediaToken) return; + keepLogin.value = remember; - storage.value.setItem('hederaLastUser', username); - setToken(data.token); - setSession({ created: Date.now(), ...data }); + setSession({ + created: Date.now(), + tokenMultimedia: multimediaToken.id, + username, + ...data + }); await fetchTokenConfig(); startInterval(); }; @@ -164,7 +197,16 @@ export const useUserStore = defineStore('user', () => { const tokenData = await api.post('VnUsers/renewToken', { headers: { Authorization: _token } }); - setToken(tokenData.data.id); + + const _tokenMultimedia = getTokenMultimedia(); + const tokenMultimedia = await api.post('VnUsers/renewToken', { + headers: { Authorization: _tokenMultimedia } + }); + + setToken({ + _token: tokenData.data.id, + _tokenMultimedia: tokenMultimedia.data.id + }); }; const checkValidity = async () => { @@ -282,6 +324,7 @@ export const useUserStore = defineStore('user', () => { userLocaleOption, storage, getToken, + getTokenMultimedia, setToken, destroyToken, destroy,