mirror of
https://git.srhtnin.garden/sarah/srhtnin.github.io.git
synced 2026-10-14 02:38:12 +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,39 @@
|
||||
<?php
|
||||
|
||||
$PageTitle = "Admin";
|
||||
$PageSection = "Admin";
|
||||
|
||||
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en" data-palette="CatppuccinMocha">
|
||||
<?php require __DIR__ . "/Bits/Head.php"; ?>
|
||||
|
||||
<body>
|
||||
<?php require __DIR__ . "/Bits/Navbar.php"; ?>
|
||||
|
||||
<main class="MainContainer">
|
||||
<h1>Admin</h1>
|
||||
|
||||
<p
|
||||
id="AdminAccessMessage"
|
||||
class="PageMessage"
|
||||
aria-live="polite"
|
||||
>
|
||||
Checking admin access...
|
||||
</p>
|
||||
|
||||
<div
|
||||
id="AdminContent"
|
||||
hidden
|
||||
>
|
||||
<p>
|
||||
Admin access verified. Garden content
|
||||
editors will live here.
|
||||
</p>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
@@ -29,4 +29,25 @@ $NavbarPages = [
|
||||
>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<a
|
||||
id="AdminNavbarLink"
|
||||
class="NavbarLink"
|
||||
href="/Pages/Admin.html"
|
||||
aria-label="Admin"
|
||||
title="Admin"
|
||||
<?= ($PageSection ?? $PageTitle ?? "") === "Admin" ? 'aria-current="page"' : "" ?>
|
||||
hidden
|
||||
>
|
||||
<img
|
||||
class="NavbarIcon"
|
||||
src="/Assets/Img/Icon.png"
|
||||
alt=""
|
||||
>
|
||||
</a>
|
||||
</nav>
|
||||
|
||||
<script
|
||||
src="/Scripts/Garden/AdminAccess.js"
|
||||
defer
|
||||
></script>
|
||||
@@ -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
|
||||
);
|
||||
@@ -300,6 +300,10 @@ select {
|
||||
border: 4px solid var(--Mauve);
|
||||
}
|
||||
|
||||
.NavbarLink[hidden] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.NavbarLink {
|
||||
display: block;
|
||||
|
||||
|
||||
Reference in new issue
Block a user