feat: handle request when unauthorized
This commit is contained in:
parent
f4c34162a6
commit
825e4497d2
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue