working on login form

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2024-07-17 00:46:44 +02:00
parent add4209a1f
commit 67606a80f4
5 changed files with 28 additions and 7 deletions

View File

@@ -17,7 +17,7 @@ public class Backend extends PathHandler {
System.out.printf("%s %s…", method, path);
if ("login".equals(path)) {
doLogin(ex);
doLogin(ex); // TODO: prevent brute force
return;
}
var token = getAuthToken(ex);

View File

@@ -1 +1,2 @@
var api = "/api";
var api = "/api";
var web = "/web";

View File

@@ -12,8 +12,26 @@ function checkUser(){
.then(handleCheckUser)
.catch((err) => console.log(err));
}
function submitForm(formId){
var data = Object.fromEntries(new FormData(document.getElementById(formId)));
function handleLogin(response){
if (response.status == 401){
loadError("login-failed");
return;
}
console.log(response);
}
function loadError(page){
fetch(web+"/"+page+".txt").then(resp => resp.text()).then(showError);
}
function showError(content){
document.getElementById("error").innerHTML = content;
}
function tryLogin(){
document.getElementById("error").innerHTML = "";
var data = Object.fromEntries(new FormData(document.getElementById('login')));
fetch(api+"/login",{
headers: {
'login-username': data.user,
@@ -21,5 +39,5 @@ function submitForm(formId){
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
}).then(handleLogin);
}

View File

@@ -0,0 +1 @@
<span>Login failed!</span>

View File

@@ -6,7 +6,7 @@
</head>
<body>
<h1>Login</h1>
<form id="form">
<form id="login">
<fieldset>
<legend>User credentials</legend>
<label>
@@ -17,8 +17,9 @@
Password
<input type="password" name="pass" />
</label>
<button type="button" onClick="submitForm('form')">Login</button>
<button type="button" onClick="tryLogin()">Login</button>
</fieldset>
</form>
<div id="error"></div>
</body>
</html>