reimbursinator/front/static/js/login.js

42 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-01-17 06:59:51 +00:00
function postToLoginEndpoint(event) {
event.preventDefault();
const credentials = {
"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!");
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);
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";
console.error("LOGIN FAILURE!");
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);