mirror of
https://git.srhtnin.garden/sarah/srhtnin.github.io.git
synced 2026-10-13 18:38:11 +02:00
Added admin-only page, and updated server backend to actually work with
admin stuff (IsAdmin).
This commit is contained in:
1 parent
2cb01a35e0
commit
953df07878
4 files changed
+197
No files matched your search
@@ -0,0 +1,133 @@
|
||||
const AdminApiUrl =
|
||||
"https://api.srhtnin.garden";
|
||||
|
||||
const AdminSaveKeyName =
|
||||
"SarahtoninGardenSaveKey";
|
||||
|
||||
|
||||
function GetStoredAdminSaveKey() {
|
||||
const SaveKey =
|
||||
localStorage.getItem(
|
||||
AdminSaveKeyName
|
||||
);
|
||||
|
||||
if (
|
||||
typeof SaveKey !== "string" ||
|
||||
!/^[0-9A-Fa-f]{64}$/.test(
|
||||
SaveKey
|
||||
)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return SaveKey;
|
||||
}
|
||||
|
||||
|
||||
async function GetAdminStatus() {
|
||||
const SaveKey =
|
||||
GetStoredAdminSaveKey();
|
||||
|
||||
if (SaveKey === null) {
|
||||
return {
|
||||
Success: true,
|
||||
IsAdmin: false
|
||||
};
|
||||
}
|
||||
|
||||
const Response = await fetch(
|
||||
AdminApiUrl +
|
||||
"/AdminStatus.php",
|
||||
{
|
||||
method: "POST",
|
||||
|
||||
headers: {
|
||||
"Content-Type":
|
||||
"application/json"
|
||||
},
|
||||
|
||||
body: JSON.stringify({
|
||||
SaveKey: SaveKey
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
const Result =
|
||||
await Response.json();
|
||||
|
||||
if (!Response.ok) {
|
||||
throw new Error(
|
||||
Result.Error ??
|
||||
"Couldn't verify admin access."
|
||||
);
|
||||
}
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
|
||||
async function RenderAdminAccess() {
|
||||
const NavbarLink =
|
||||
document.getElementById(
|
||||
"AdminNavbarLink"
|
||||
);
|
||||
|
||||
const AdminContent =
|
||||
document.getElementById(
|
||||
"AdminContent"
|
||||
);
|
||||
|
||||
const AdminMessage =
|
||||
document.getElementById(
|
||||
"AdminAccessMessage"
|
||||
);
|
||||
|
||||
|
||||
try {
|
||||
const Status =
|
||||
await GetAdminStatus();
|
||||
|
||||
const IsAdmin =
|
||||
Status.Success === true &&
|
||||
Status.IsAdmin === true;
|
||||
|
||||
|
||||
if (IsAdmin) {
|
||||
if (NavbarLink !== null) {
|
||||
NavbarLink.hidden = false;
|
||||
}
|
||||
|
||||
if (AdminContent !== null) {
|
||||
AdminContent.hidden = false;
|
||||
}
|
||||
|
||||
if (AdminMessage !== null) {
|
||||
AdminMessage.hidden = true;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (AdminMessage !== null) {
|
||||
AdminMessage.textContent =
|
||||
"Admin access required.";
|
||||
}
|
||||
} catch (Error) {
|
||||
console.error(
|
||||
"Couldn't verify admin access:",
|
||||
Error
|
||||
);
|
||||
|
||||
if (AdminMessage !== null) {
|
||||
AdminMessage.textContent =
|
||||
"Couldn't verify admin access.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
document.addEventListener(
|
||||
"DOMContentLoaded",
|
||||
RenderAdminAccess
|
||||
);
|
||||
Reference in new issue
Block a user