feat: handle request when unauthorized
This commit is contained in:
parent
f4c34162a6
commit
825e4497d2
|
@ -58,31 +58,37 @@ export function useSession() {
|
|||
}
|
||||
}
|
||||
}
|
||||
async function destroy() {
|
||||
async function destroy(destroyTokens = true) {
|
||||
const tokens = {
|
||||
tokenMultimedia: 'Accounts/logout',
|
||||
token: 'VnUsers/logout',
|
||||
};
|
||||
const storage = keepLogin() ? localStorage : sessionStorage;
|
||||
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();
|
||||
|
||||
for (const [key, url] of Object.entries(tokens)) {
|
||||
await destroyToken(url, storage, key);
|
||||
setUser({
|
||||
id: 0,
|
||||
name: '',
|
||||
nickname: '',
|
||||
lang: '',
|
||||
darkMode: null,
|
||||
});
|
||||
|
||||
stopRenewer();
|
||||
}
|
||||
|
||||
localStorage.clear();
|
||||
sessionStorage.clear();
|
||||
|
||||
const { setUser } = useState();
|
||||
|
||||
setUser({
|
||||
id: 0,
|
||||
name: '',
|
||||
nickname: '',
|
||||
lang: '',
|
||||
darkMode: null,
|
||||
});
|
||||
|
||||
stopRenewer();
|
||||
}
|
||||
|
||||
async function login(data) {
|
||||
|
|
Loading…
Reference in New Issue