start work on persistent login

This commit is contained in:
2026-07-21 12:06:46 +10:00
parent cca6737791
commit 4ce1d56d69
8 changed files with 255 additions and 6 deletions

View File

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

33
client/login.html Normal file
View File

@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>login</title>
<link rel="stylesheet" type="text/css" href="/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/script.js"></script>
</head>
<body>
<nav>
<div id="brand">
<h2><a href="/">chookchat</a></h2>
</div>
<ul>
<li><a href="/">home</a></li>
<li><a id="loginLink" href="/login">login</a></li>
</ul>
</nav>
<div class="login" id="posts">
<h1>login</h1>
<p>if you haven't registered, enter a username that doesn't exist and hit 'register'</p>
<label for="username">Username:</label>
<input name="username" id="username"></input>
<br>
<label for="password">Password:</label>
<input type="password" name="password" id="password"></input>
<br>
<button onclick="login()">Log In</button>
<button onclick="register()">Register</button>
</div>
</body>
</html>

View File

@@ -1,10 +1,5 @@
async function like(id) {
const usernameBox = document.getElementById('username');
const passwordBox = document.getElementById('password');
const formData = new FormData();
formData.append('username', usernameBox.value);
formData.append('password', passwordBox.value);
formData.append('post', id);
const result = await fetch('/like', {
@@ -12,7 +7,60 @@ async function like(id) {
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 = "/";
}