2019-01-17 06:59:51 +00:00
|
|
|
function postToLoginEndpoint(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const credentials = {
|
2019-02-06 23:31:15 +00:00
|
|
|
"email" : this.elements.email.value,
|
2019-01-17 06:59:51 +00:00
|
|
|
"password" : this.elements.password.value
|
|
|
|
}
|
2019-02-05 09:05:04 +00:00
|
|
|
const url = "https://" + window.location.hostname + ":8444/api/v1/account/login/";
|
2019-01-17 06:59:51 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
2019-02-05 09:05:04 +00:00
|
|
|
console.log("Attempting a connection to the following endpoint: " + url);
|
2019-02-02 06:29:11 +00:00
|
|
|
console.log("User credentials:\n" + JSON.stringify(credentials));
|
2019-01-17 06:59:51 +00:00
|
|
|
|
|
|
|
xhr.open("POST", url, true);
|
|
|
|
xhr.setRequestHeader("Content-Type", "application/json");
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (this.readyState === 4) {
|
|
|
|
if (this.status === 200) {
|
|
|
|
console.log("LOGIN SUCCESS!");
|
2019-02-02 06:24:18 +00:00
|
|
|
console.log("Server response:\n" + this.response);
|
2019-02-05 09:05:04 +00:00
|
|
|
token = JSON.parse(this.response).key;
|
2019-01-17 06:59:51 +00:00
|
|
|
localStorage.setItem("token", token);
|
2019-01-30 17:03:11 +00:00
|
|
|
window.location.replace("home.html");
|
2019-01-17 06:59:51 +00:00
|
|
|
} else {
|
2019-02-05 19:28:58 +00:00
|
|
|
document.getElementById("errorLogin").innerHTML = "Incorrect user name or password";
|
2019-01-30 17:03:11 +00:00
|
|
|
console.error("LOGIN FAILURE!");
|
2019-02-02 06:24:18 +00:00
|
|
|
console.error("Server status: " + this.status);
|
|
|
|
console.error("Server response:\n" + this.response);
|
2019-01-17 06:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onerror = function() {
|
|
|
|
alert("Error connecting to the authentication server!");
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(JSON.stringify(credentials));
|
|
|
|
}
|
|
|
|
|
|
|
|
const form = document.querySelector("form");
|
|
|
|
form.addEventListener("submit", postToLoginEndpoint);
|