reimbursinator/front/static/js/login.js

46 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-01-17 06:59:51 +00:00
function displayErrorMessage(errorMessage) {
const errorReport = document.querySelector("#errorReport");
errorReport.innerHTML = JSON.parse(errorMessage).error;
}
function postToLoginEndpoint(event) {
event.preventDefault();
const credentials = {
"username" : this.elements.username.value,
"password" : this.elements.password.value
}
const url = "https://reqres.in/api/login" // mock api service
const xhr = new XMLHttpRequest();
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).token;
localStorage.setItem("token", token);
window.location.replace("home.html");
2019-01-17 06:59:51 +00:00
} else {
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
displayErrorMessage(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);