reimbursinator/front/static/js/login.js
2019-02-05 10:12:30 -08:00

40 lines
1.4 KiB
JavaScript

function postToLoginEndpoint(event) {
event.preventDefault();
const credentials = {
"username" : this.elements.username.value,
"password" : this.elements.password.value
}
const url = "https://" + window.location.hostname + ":8444/api/v1/account/login/";
const xhr = new XMLHttpRequest();
console.log("Attempting a connection to the following endpoint: " + url);
console.log("User credentials:\n" + JSON.stringify(credentials));
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);
token = JSON.parse(this.response).key;
localStorage.setItem("token", token);
window.location.replace("home.html");
} else {
console.error("LOGIN FAILURE!");
console.error("Server status: " + this.status);
console.error("Server response:\n" + this.response);
}
}
};
xhr.onerror = function() {
alert("Error connecting to the authentication server!");
};
xhr.send(JSON.stringify(credentials));
}
const form = document.querySelector("form");
form.addEventListener("submit", postToLoginEndpoint);