feat: handle request when unauthorized

This commit is contained in:
Javier Segarra 2024-07-03 14:18:24 +02:00
parent f4c34162a6
commit 825e4497d2
1 changed files with 24 additions and 18 deletions

View File

@ -58,20 +58,25 @@ export function useSession() {
}
}
}
async function destroy() {
async function destroy(destroyTokens = true) {
const tokens = {
tokenMultimedia: 'Accounts/logout',
token: 'VnUsers/logout',
};
const storage = keepLogin() ? localStorage : sessionStorage;
for (const [key, url] of Object.entries(tokens)) {
await destroyToken(url, storage, key);
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();
setUser({
@ -84,6 +89,7 @@ export function useSession() {
stopRenewer();
}
}
async function login(data) {
setSession(data);