68 lines
1.3 KiB
JavaScript
68 lines
1.3 KiB
JavaScript
const username = getID("username")
|
|
const password = getID("password")
|
|
const submit = getID("submit")
|
|
|
|
const loginInfo = new TextResponse("login-info")
|
|
|
|
loginInfo.clear()
|
|
|
|
submit.addEventListener("click", () => {
|
|
|
|
if(!username.value) {
|
|
|
|
loginInfo.err("Un nom d'utilisateur est nécéssaire pour se connecter !")
|
|
|
|
} else if(!password.value) {
|
|
|
|
loginInfo.err("Un mot de passe est nécéssaire pour se connecter !")
|
|
} else {
|
|
|
|
|
|
login()
|
|
}
|
|
})
|
|
|
|
password.addEventListener("keyup", (event) => {
|
|
if (event.key === "Enter") {
|
|
login()
|
|
}
|
|
});
|
|
|
|
function login() {
|
|
|
|
loginInfo.clear()
|
|
|
|
fetch('/login', {
|
|
method: 'POST',
|
|
redirect: 'follow',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
username: username.value,
|
|
password: password.value
|
|
})
|
|
})
|
|
.then(response => redirect(response))
|
|
|
|
async function redirect(response) {
|
|
|
|
response = await response.text()
|
|
|
|
if(response == "AUTH_FAILED") {
|
|
|
|
|
|
loginInfo.err("Le nom d'utilisateur et le mot de passe sont incorrects.")
|
|
|
|
} else if(response == "AUTH_SUCCESS") {
|
|
|
|
window.location.href = "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|