kkk
This commit is contained in:
parent
441f00507d
commit
a0b5e97058
3 changed files with 52 additions and 23 deletions
|
|
@ -144,32 +144,26 @@ async function authedAPIRequest(endpoint, options) {
|
|||
}
|
||||
|
||||
options.headers.Authorization = "Bearer " + localStorage.getItem("token");
|
||||
options.mode = "cors";
|
||||
|
||||
const full_endpoint = import.meta.env.VITE_URL + "/api" + endpoint;
|
||||
let response;
|
||||
|
||||
try {
|
||||
response = await fetch(full_endpoint, options);
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
} catch (err) {
|
||||
if (response.status === 401) {
|
||||
const response_err = await response.text();
|
||||
response = await fetch(full_endpoint, options);
|
||||
if (response.status === 401) {
|
||||
const response_err = await response.text();
|
||||
|
||||
if (response_err.includes("Token expired")) {
|
||||
await refreshToken(null);
|
||||
return null;
|
||||
if (response_err.includes("Token expired")) {
|
||||
if (await refreshToken(null)) {
|
||||
return await authedAPIRequest(endpoint, options);
|
||||
}
|
||||
|
||||
localStorage.clear();
|
||||
alert("Redirecting to logging page");
|
||||
redirect.redirectToLoginPage();
|
||||
return null;
|
||||
}
|
||||
|
||||
console.error(err);
|
||||
localStorage.clear();
|
||||
alert("Redirecting to logging page");
|
||||
redirect.redirectToLoginPage();
|
||||
return null;
|
||||
}
|
||||
|
||||
return response;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue