0
1
Fork 0

Add multimedia token feature

This commit is contained in:
William Buezas 2024-10-07 09:14:59 -03:00
parent 56cf0f0e64
commit b669adab5a
1 changed files with 51 additions and 8 deletions

View File

@ -7,9 +7,12 @@ import { useAppStore } from 'src/stores/app.js';
const { t } = i18n.global; const { t } = i18n.global;
const { notify } = useNotify(); const { notify } = useNotify();
const TOKEN_MULTIMEDIA = 'tokenMultimedia';
const TOKEN = 'token';
export const useUserStore = defineStore('user', () => { export const useUserStore = defineStore('user', () => {
const token = ref(''); const token = ref('');
const tokenMultimedia = ref('');
const isGuest = ref(false); const isGuest = ref(false);
const user = ref(null); // Usuario en uso => supplantedUser || mainUser const user = ref(null); // Usuario en uso => supplantedUser || mainUser
const supplantedUser = ref(null); // Usuario suplantado const supplantedUser = ref(null); // Usuario suplantado
@ -54,15 +57,24 @@ export const useUserStore = defineStore('user', () => {
const getToken = () => { const getToken = () => {
const tokenValue = const tokenValue =
sessionStorage.getItem('vnToken') || sessionStorage.getItem(TOKEN) || localStorage.getItem(TOKEN);
localStorage.getItem('vnToken');
token.value = tokenValue; token.value = tokenValue;
return tokenValue; return tokenValue;
}; };
const setToken = _token => { const getTokenMultimedia = () => {
storage.value.setItem('vnToken', _token); 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; token.value = _token;
tokenMultimedia.value = _tokenMultimedia;
}; };
const destroyToken = async (url, storageType, key) => { const destroyToken = async (url, storageType, key) => {
@ -106,18 +118,39 @@ export const useUserStore = defineStore('user', () => {
}; };
const setSession = data => { 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('created', data.created);
storage.value.setItem('ttl', data.ttl); storage.value.setItem('ttl', data.ttl);
localStorage.setItem('keepLogin', keepLogin.value); 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 login = async (username, password, remember) => {
const params = { user: username, password }; const params = { user: username, password };
const { data } = await api.post('Accounts/login', params); const { data } = await api.post('Accounts/login', params);
const multimediaToken = await fetchMultimediaToken(data);
if (!multimediaToken) return;
keepLogin.value = remember; keepLogin.value = remember;
storage.value.setItem('hederaLastUser', username); setSession({
setToken(data.token); created: Date.now(),
setSession({ created: Date.now(), ...data }); tokenMultimedia: multimediaToken.id,
username,
...data
});
await fetchTokenConfig(); await fetchTokenConfig();
startInterval(); startInterval();
}; };
@ -164,7 +197,16 @@ export const useUserStore = defineStore('user', () => {
const tokenData = await api.post('VnUsers/renewToken', { const tokenData = await api.post('VnUsers/renewToken', {
headers: { Authorization: _token } 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 () => { const checkValidity = async () => {
@ -282,6 +324,7 @@ export const useUserStore = defineStore('user', () => {
userLocaleOption, userLocaleOption,
storage, storage,
getToken, getToken,
getTokenMultimedia,
setToken, setToken,
destroyToken, destroyToken,
destroy, destroy,