Logins + sessions working

This commit is contained in:
2026-07-21 16:05:08 +10:00
parent c6a0e91e7e
commit 7e9271db72
3 changed files with 110 additions and 8 deletions

View File

@@ -6,7 +6,6 @@
</div>
<ul>
<li><a href="/">home</a></li>
<li><a id="loginLink" href="/login">login</a></li>
</ul>
</nav>

View File

@@ -18,6 +18,25 @@ async function like(id) {
}
}
async function post() {
const postbox = document.getElementById('newpost');
const formData = new FormData();
formData.append('post', postbox.value);
const result = await fetch('/post', {
method: 'POST',
body: formData
});
if (result.status < 200 || result.status >= 400) {
alert("you have been silenced by the server and your post did not go through");
return;
}
window.location.href = "/";
}
async function register() {
const usernameBox = document.getElementById('username');
const passwordBox = document.getElementById('password');
@@ -48,7 +67,6 @@ async function login() {
formData.append('username', usernameBox.value);
formData.append('password', passwordBox.value);
console.log(formData);
const result = await fetch('/login', {
method: 'POST',
@@ -57,8 +75,6 @@ async function login() {
if (result.status < 200 || result.status >= 400) {
alert("for some reason the server hates you and didn't let you log into your account");
console.log(result.status);
console.log(result.content);
return;
}
@@ -78,13 +94,19 @@ async function checkLoginStatus() {
document.addEventListener('DOMContentLoaded', async function() {
if (window.location.pathname == "/" && window.fetch) {
if (window.fetch) {
// We know the fetch API exists, so hide the stuff that doesn't rely on it
const form = document.getElementById("postbox-form");
if (!form) {
return;
}
form.style.display = 'none';
const postbox = document.getElementById("postbox");
if (!postbox) {
return;
}
const userStatus = await checkLoginStatus();
@@ -98,8 +120,20 @@ document.addEventListener('DOMContentLoaded', async function() {
const helperText = document.createElement("p");
helperText.textContent = "You're posting as " + userStatus;
postbox.appendChild(helperText);
const textArea = document.createElement("textarea");
textArea.id = "newpost";
postbox.appendChild(textArea);
const postButton = document.createElement("button");
postButton.textContent = "Post";
postButton.id = "postbutton"
postButton.onclick = post;
postbox.appendChild(postButton);
const loginLink = document.getElementById("loginLink");
loginLink.textContent = "hello, " + userStatus + "!";
loginLink.href = "/profile/" + userStatus;
}
}