diff --git a/client/header_bottom.html b/client/header_bottom.html index 866da5e..76669a9 100644 --- a/client/header_bottom.html +++ b/client/header_bottom.html @@ -11,8 +11,8 @@ -
-
+
+
diff --git a/client/script.js b/client/script.js index 26604f4..3faf8c4 100644 --- a/client/script.js +++ b/client/script.js @@ -64,3 +64,44 @@ async function login() { window.location.href = "/"; } + +async function checkLoginStatus() { + const result = await fetch('/me'); + + if (result.headers.get('X-Logged-In') !== 'true') { + return ""; + } + + const username = result.headers.get('X-Username'); + return username; +} + +document.addEventListener('DOMContentLoaded', async function() { + + if (window.location.pathname == "/" && window.fetch) { + // We know the fetch API exists, so hide the stuff that doesn't rely on it + + const form = document.getElementById("postbox-form"); + form.style.display = 'none'; + + const postbox = document.getElementById("postbox"); + + const userStatus = await checkLoginStatus(); + + // if we're logged in, show the post box, + // otherwise show a welcome message, encouraging users to sign up/log in + if (userStatus === "") { + const newElement = document.createElement("p"); + newElement.textContent = "Welcome to Chookchat!\nTo get posting, click 'login' to log in or create an account.\nEnjoy your stay!"; + postbox.appendChild(newElement); + } else { + const helperText = document.createElement("p"); + helperText.textContent = "You're posting as " + userStatus; + postbox.appendChild(helperText); + const textArea = document.createElement("textarea"); + postbox.appendChild(textArea); + } + + } + +}); diff --git a/server/src/main.cpp b/server/src/main.cpp index deeadde..0f5e1b8 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -232,6 +232,19 @@ int main() { } }); + svr.Get("/me", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) { + std::optional user = getLoggedInUser(request, database); + + if (!user.has_value()) { + response.set_header("X-Logged-In", "false"); + return; + } + + response.set_header("X-Logged-In", "false"); + response.set_header("X-Username", user->name); + + }); + svr.listen("0.0.0.0", 8080); }