neutral-old/public/javascripts/loginscript.js

82 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-02-18 21:09:04 +00:00
const userField = document.getElementById("username");
const passwordField = document.getElementById("password");
const remindus = document.getElementById("remindus");
const loginButton = document.getElementById("loginButton");
const info = document.getElementById("info");
2023-02-21 11:02:37 +00:00
userField.addEventListener("keyup", (event) => {
if (event.key === "Enter") {
passwordField.value = null;
passwordField.focus()
}
});
passwordField.addEventListener("keyup", (event) => {
if (event.key === "Enter") {
login()
}
});
function login() {
2023-02-18 21:09:04 +00:00
info.innerHTML = ""
const userValue = userField.value;
const passwordValue = passwordField.value;
if(userValue == "" | passwordValue == "") {
info.innerHTML = "Tous les champs doivent être remplis."
} else if(userValue.includes(" ")) {
info.innerHTML = "Le nom d'utilisateur ne peut pas contenir un espace"
} else {
const loginData = {
"username":userValue,
"password": passwordValue,
"remindus": remindus.checked
}
fetch('/login', {
method: 'POST',
redirect: 'follow',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(loginData)
})
.then(response => response.json())
.then(response => redirect(response))
function redirect(response) {
if(response.token == "auth_failed") {
2023-02-21 11:02:37 +00:00
passwordField.value = null;
2023-02-18 21:09:04 +00:00
info.innerHTML = "Le nom d'utilisateur ou le mot de passe est éronné"
2023-02-21 11:02:37 +00:00
2023-02-18 21:09:04 +00:00
} else if(response.token == "auth_success") {
window.location.href = "/"
}
}
}
2023-02-21 11:02:37 +00:00
}
loginButton.addEventListener("click", () => {
login()
2023-02-18 21:09:04 +00:00
});