async function like(id) { const formData = new FormData(); formData.append('post', id); const result = await fetch('/like', { method: 'POST', body: formData }); if (result.status === 401) { alert("you need to login before you like a post"); return; } if (result.status < 200 || result.status >= 400) { alert("your post didnt get the like because the server thought your opinion was invalid"); return; } } async function register() { const usernameBox = document.getElementById('username'); const passwordBox = document.getElementById('password'); const formData = new FormData(); formData.append('username', usernameBox.value); formData.append('password', passwordBox.value); const result = await fetch('/register', { method: 'POST', body: formData }); if (result.status < 200 || result.status >= 400) { alert("fard"); return; } window.location.href = "/"; } async function login() { const usernameBox = document.getElementById('username'); const passwordBox = document.getElementById('password'); const formData = new FormData(); formData.append('username', usernameBox.value); formData.append('password', passwordBox.value); console.log(formData); const result = await fetch('/login', { method: 'POST', body: formData }); 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; } 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); } } });