a few qol fixes

This commit is contained in:
2026-07-21 20:04:33 +10:00
parent dd7e1f8981
commit e99d312937
2 changed files with 33 additions and 21 deletions

View File

@@ -85,36 +85,53 @@ async function checkLoginStatus() {
const result = await fetch('/me');
if (result.headers.get('X-Logged-In') !== 'true') {
return "";
return {
username: "",
bio: ""
};
}
const username = result.headers.get('X-Username');
return username;
const obj = {
username: result.headers.get('X-Username'),
bio: result.headers.get('X-Bio')
};
return obj;
}
document.addEventListener('DOMContentLoaded', async function() {
if (window.fetch) {
// We know the fetch API exists, so hide the stuff that doesn't rely on it
const userStatus = await checkLoginStatus();
// update nav link / redirect regardless of whether a postbox exists
const loginLink = document.getElementById("loginLink");
if (userStatus.username === "") {
if (window.location.pathname === "/settings") {
window.location.href = "/login";
return;
}
} else if (loginLink) {
loginLink.textContent = "hello, " + userStatus.username + "!";
loginLink.href = "/profile/" + userStatus.username;
}
if (userStatus.username !== "" && window.location.pathname === "/settings") {
// fill bio box with the bio
const textarea = document.getElementById("change-bio");
textarea.value = userStatus.bio;
}
const postbox = document.getElementById("postbox");
if (!postbox) {
return;
}
// if we're logged in, show the post box,
// otherwise show a welcome message, encouraging users to sign up/log in
if (userStatus === "") {
if (window.location.pathname === "/settings") {
window.location.href = "/login";
}
// postbox-specific setup stays here
if (userStatus.username === "") {
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;
helperText.textContent = "You're posting as " + userStatus.username;
postbox.appendChild(helperText);
const textArea = document.createElement("textarea");
@@ -123,13 +140,9 @@ document.addEventListener('DOMContentLoaded', async function() {
const postButton = document.createElement("button");
postButton.textContent = "Post";
postButton.id = "postbutton"
postButton.id = "postbutton";
postButton.onclick = post;
postbox.appendChild(postButton);
const loginLink = document.getElementById("loginLink");
loginLink.textContent = "hello, " + userStatus + "!";
loginLink.href = "/profile/" + userStatus;
}
const form = document.getElementById("postbox-form");
@@ -137,9 +150,7 @@ document.addEventListener('DOMContentLoaded', async function() {
return;
}
form.style.display = 'none';
}
});
async function settingsChangePassword() {

View File

@@ -362,6 +362,7 @@ int main() {
response.set_header("X-Logged-In", "true");
response.set_header("X-Username", user->name);
response.set_header("X-Bio", user->bio);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");