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

View File

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