reimbursinator/front/static/js/logout.js

34 lines
1.1 KiB
JavaScript
Raw Normal View History

function postToLogoutEndpoint(event) {
event.preventDefault();
const token = localStorage.getItem("token");
2019-02-05 09:05:04 +00:00
const url = "https://" + window.location.hostname + ":8444/api/v1/account/logout/";
const xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
2019-02-05 09:05:04 +00:00
xhr.setRequestHeader("Authorization", "Bearer " + token);
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
console.log("LOGOUT SUCCESS!");
console.log("Server response:\n" + this.response);
localStorage.removeItem("token");
2019-02-05 09:05:04 +00:00
window.location.replace("/");
} else {
console.error("LOGOUT FAILURE!");
console.error("Server status: " + this.status);
console.error("Server response:\n" + this.response);
}
}
};
xhr.onerror = function() {
alert("Error connecting to authentication server!");
};
xhr.send();
}
const logoutLink = document.querySelector(".log-out-link");
logoutLink.addEventListener("click", postToLogoutEndpoint);