Added admin-only page, and updated server backend to actually work with

admin stuff (IsAdmin).
This commit is contained in:
sarah committed 2026-09-01 20:01:11 +02:00
1 parent 2cb01a35e0
commit 953df07878
4 files changed
+197

No files matched your search

+133
View File
@@ -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
);