try to fix issues with logging in on linux (again)

This commit is contained in:
2026-05-06 11:51:53 +02:00
parent 524a46f583
commit a74a9995cd
3 changed files with 99 additions and 29 deletions
+74 -27
View File
@@ -1,42 +1,89 @@
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
const form = document.querySelector("form"); const form = document.querySelector("form");
const registerButton = document.getElementById("register-button");
const loginButton = document.getElementById("login-button");
const authError = document.getElementById("auth-error");
form.addEventListener("submit", async (event) => {
event.preventDefault();
await authenticate("/login", "Login successful. Loading Lobby...");
});
registerButton.addEventListener("click", async () => {
await authenticate("/register", "Register successful. Loading Lobby...");
});
async function authenticate(endpoint, successMessage) {
clearAuthError();
setButtonsDisabled(true);
const credentials = {
username: form.username.value,
password: form.password.value
};
form.addEventListener("submit", async (e) => {
e.preventDefault();
const username = form.username.value;
const password = form.password.value;
const credentials = {username, password};
try { try {
const loginRes = await fetch("/login", { const response = await fetch(endpoint, {
method: "POST", method: "POST",
headers: {"Content-Type": "application/json"}, headers: {"Content-Type": "application/json"},
body: JSON.stringify(credentials), credentials: "same-origin",
body: JSON.stringify(credentials)
}); });
if (loginRes.ok) { const data = await readJsonResponse(response);
await loginRes.json(); if (!response.ok) {
showLoadingScreen("Login successful. Loading Lobby..."); showAuthError(data.error || "Authentication failed.");
return; return;
} }
const registerRes = await fetch("/register", { const verified = await verifySession();
method: "POST", if (!verified) {
headers: {"Content-Type": "application/json"}, showAuthError("Login succeeded, but the browser did not send the session cookie back to the server.");
body: JSON.stringify(credentials), return;
});
if (registerRes.ok) {
await registerRes.json();
showLoadingScreen("Register successful. Loading Lobby...");
} else {
const error = await registerRes.json();
alert("Register failed: " + (error.error || "Unknown error"));
} }
} catch (err) {
console.error("Network error:", err); showLoadingScreen(successMessage);
alert("Server unreachable."); } catch (error) {
console.error("Authentication request failed:", error);
showAuthError("Server unreachable.");
} finally {
setButtonsDisabled(false);
} }
}); }
async function verifySession() {
const response = await fetch("/verify", {
method: "POST",
credentials: "same-origin"
});
if (!response.ok) return false;
const data = await readJsonResponse(response);
return Boolean(data.valid && data.user?.username);
}
async function readJsonResponse(response) {
try {
return await response.json();
} catch {
return {};
}
}
function showAuthError(message) {
authError.textContent = message;
authError.classList.remove("hidden");
}
function clearAuthError() {
authError.textContent = "";
authError.classList.add("hidden");
}
function setButtonsDisabled(disabled) {
loginButton.disabled = disabled;
registerButton.disabled = disabled;
}
function showLoadingScreen(message) { function showLoadingScreen(message) {
const loadingScreen = document.getElementById("loading-screen"); const loadingScreen = document.getElementById("loading-screen");
+6 -2
View File
@@ -11,7 +11,11 @@
<form method="post" action="/login"> <form method="post" action="/login">
<input type="text" name="username" placeholder="Username"> <input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password"> <input type="password" name="password" placeholder="Password">
<button type="submit">Login</button> <div class="auth-actions">
<button type="submit" id="login-button">Login</button>
<button type="button" id="register-button">Register</button>
</div>
<p id="auth-error" class="auth-error hidden"></p>
</form> </form>
<div id="loading-screen" class="hidden"> <div id="loading-screen" class="hidden">
<div class="loading-text" id="loading-text">Loading...</div> <div class="loading-text" id="loading-text">Loading...</div>
@@ -21,4 +25,4 @@
</div> </div>
</body> </body>
<script src="frontend_login.js"></script> <script src="frontend_login.js"></script>
</html> </html>
+19
View File
@@ -63,6 +63,25 @@ button:hover {
background: #1f1; background: #1f1;
} }
button:disabled {
cursor: not-allowed;
opacity: 0.6;
}
.auth-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.auth-error {
color: #ff6b6b;
font-size: 14px;
line-height: 1.4;
margin: 0;
text-align: center;
}
.hidden { .hidden {
display: none !important; display: none !important;
} }