continue working on login

This commit is contained in:
2026-07-21 13:09:52 +10:00
parent 4ce1d56d69
commit a7dbe2fca3
3 changed files with 56 additions and 2 deletions

View File

@@ -11,8 +11,8 @@
</ul>
</nav>
<div class="postbox">
<form action="/make_post" method="post">
<div class="postbox" id="postbox">
<form id="postbox-form" action="/make_post" method="post">
<div>
<label for="username">Username:</label>
<input name="username" id="username" />

View File

@@ -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);
}
}
});

View File

@@ -232,6 +232,19 @@ int main() {
}
});
svr.Get("/me", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::optional<User> 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);
}