This commit is contained in:
1 parent
0cf6f62e23
commit
0c12ce5f33
5 files changed
+109
-1
No files matched your search
@@ -32,6 +32,15 @@
|
||||
return `./login.html?next=${encodeURIComponent(current)}`;
|
||||
}
|
||||
|
||||
function isUnauthorizedStatus(status) {
|
||||
return Number(status) === 401;
|
||||
}
|
||||
|
||||
function redirectToLogin() {
|
||||
clearSession();
|
||||
window.location.href = getLoginUrl();
|
||||
}
|
||||
|
||||
async function apiRequest(path, options = {}) {
|
||||
const headers = new Headers(options.headers || {});
|
||||
if (options.body && !(options.body instanceof FormData) && !headers.has("Content-Type")) {
|
||||
@@ -49,6 +58,9 @@
|
||||
} catch {
|
||||
message = response.statusText || message;
|
||||
}
|
||||
if (isUnauthorizedStatus(response.status)) {
|
||||
redirectToLogin();
|
||||
}
|
||||
throw new Error(message);
|
||||
}
|
||||
return response.json();
|
||||
@@ -106,6 +118,7 @@
|
||||
getLoginUrl,
|
||||
getStoredUser,
|
||||
getToken,
|
||||
isUnauthorizedStatus,
|
||||
login,
|
||||
logout,
|
||||
register,
|
||||
@@ -119,6 +132,7 @@
|
||||
getLoginUrlForPath(path) {
|
||||
return `./login.html?next=${encodeURIComponent(path)}`;
|
||||
},
|
||||
isUnauthorizedStatus,
|
||||
shouldShowAdminEntry(user) {
|
||||
return user?.role === "admin";
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user