2019-01-17 21:12:17 +00:00
|
|
|
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/";
|
2019-01-17 21:12:17 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open("POST", url, true);
|
2019-02-05 09:05:04 +00:00
|
|
|
xhr.setRequestHeader("Authorization", "Bearer " + token);
|
2019-01-17 21:12:17 +00:00
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (this.readyState === 4) {
|
|
|
|
if (this.status === 200) {
|
|
|
|
console.log("LOGOUT SUCCESS!");
|
2019-02-02 06:24:18 +00:00
|
|
|
console.log("Server response:\n" + this.response);
|
2019-01-17 21:12:17 +00:00
|
|
|
localStorage.removeItem("token");
|
2019-02-05 09:05:04 +00:00
|
|
|
window.location.replace("/");
|
2019-01-17 21:12:17 +00:00
|
|
|
} else {
|
2019-02-02 06:24:18 +00:00
|
|
|
console.error("LOGOUT FAILURE!");
|
|
|
|
console.error("Server status: " + this.status);
|
|
|
|
console.error("Server response:\n" + this.response);
|
2019-01-17 21:12:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onerror = function() {
|
|
|
|
alert("Error connecting to authentication server!");
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:46:11 +00:00
|
|
|
const logoutLink = document.querySelector(".log-out-link");
|
|
|
|
logoutLink.addEventListener("click", postToLogoutEndpoint);
|