diff --git a/Pages/Admin.php b/Pages/Admin.php new file mode 100644 index 0000000..d93fc3f --- /dev/null +++ b/Pages/Admin.php @@ -0,0 +1,39 @@ + + + + + + + + + + +
+

Admin

+ +

+ Checking admin access... +

+ + +
+ + diff --git a/Pages/Bits/Navbar.php b/Pages/Bits/Navbar.php index 91051b0..48c717f 100644 --- a/Pages/Bits/Navbar.php +++ b/Pages/Bits/Navbar.php @@ -29,4 +29,25 @@ $NavbarPages = [ > + + + hidden + > + + + + diff --git a/Scripts/Garden/AdminAccess.js b/Scripts/Garden/AdminAccess.js new file mode 100644 index 0000000..fb3d522 --- /dev/null +++ b/Scripts/Garden/AdminAccess.js @@ -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 +); diff --git a/Styles/Style.css b/Styles/Style.css index c8dacc9..2535607 100644 --- a/Styles/Style.css +++ b/Styles/Style.css @@ -300,6 +300,10 @@ select { border: 4px solid var(--Mauve); } +.NavbarLink[hidden] { + display: none; +} + .NavbarLink { display: block;