commit 18/12/2025
This commit is contained in:
54
html/webpage/assets/js/login.js
Normal file
54
html/webpage/assets/js/login.js
Normal file
@@ -0,0 +1,54 @@
|
||||
function cleartext() {
|
||||
$('#usernametext').val('');
|
||||
$('#passwordtext').val('');
|
||||
}
|
||||
|
||||
function validstring(str) {
|
||||
if (str !== null && str.length > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
console.log("Login page is ready.");
|
||||
cleartext();
|
||||
$('#loginbtn').off('click').on('click', function () {
|
||||
var username = $('#usernametext').val();
|
||||
var password = $('#passwordtext').val();
|
||||
if (!validstring(username) || !validstring(password)) {
|
||||
alert("Please enter both username and password.");
|
||||
cleartext();
|
||||
return;
|
||||
}
|
||||
fetch('/login.html', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
username: username,
|
||||
password: password
|
||||
})
|
||||
})
|
||||
.then(response => {
|
||||
//console.log("Received response from server : " + response.status + " " + response.statusText+" is redirected: "+response.redirected);
|
||||
if (response.redirected) {
|
||||
//alert("Login successful, will redirect now.");
|
||||
//console.log("Redirecting to: " + response.url);
|
||||
window.location.href = response.url;
|
||||
return;
|
||||
}
|
||||
if (response.status === 400 || response.status === 401) {
|
||||
alert("Login failed");
|
||||
cleartext();
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert("An error occurred during login.");
|
||||
cleartext();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user