new laptop setup
This commit is contained in:
Executable
+36
@@ -0,0 +1,36 @@
|
||||
const loginButton = document.getElementById('loginButton');
|
||||
const username = document.getElementById(
|
||||
'defaultLoginFormUsername');
|
||||
const password = document.getElementById(
|
||||
'defaultLoginFormPassword');
|
||||
const message = document.getElementById("loginMessage");
|
||||
|
||||
|
||||
loginButton.addEventListener("click", () => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/api/v1/auth/login", true);
|
||||
xhr.setRequestHeader("Content-Type",
|
||||
"application/json");
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState === 4) {
|
||||
if (xhr.status === 200) {
|
||||
let token = xhr.getResponseHeader("token");
|
||||
localStorage.setItem("user-token", token);
|
||||
console.log("status 200: " + document.location.origin);
|
||||
window.location.replace(
|
||||
document.location.origin);
|
||||
} else {
|
||||
message.innerText =
|
||||
"login failed please try again";
|
||||
}
|
||||
}
|
||||
};
|
||||
let data = JSON.stringify({
|
||||
"username": username.value,
|
||||
"password": password.value
|
||||
});
|
||||
|
||||
xhr.send(data);
|
||||
message.innerText = "logging in";
|
||||
|
||||
})
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
if (localStorage.getItem("user-token") == null) {
|
||||
window.location.replace(document.location.origin + "/login");
|
||||
} else {
|
||||
getArticles();
|
||||
}
|
||||
|
||||
function apiCall(url, method) {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.withCredentials = true;
|
||||
|
||||
xhr.addEventListener("readystatechange", function () {
|
||||
if (this.readyState === this.DONE) {
|
||||
if (this.status === 401) {
|
||||
window.location.replace(document.location.origin + "/login/");
|
||||
} else {
|
||||
runRenderProcess(JSON.parse(this.responseText));
|
||||
localStorage.setItem("item-cache-date", new Date());
|
||||
localStorage.setItem("item-cache-data", this.responseText);
|
||||
}
|
||||
}
|
||||
});
|
||||
xhr.open(method, "/api/v1" + url);
|
||||
xhr.setRequestHeader("content-type", "application/json");
|
||||
xhr.setRequestHeader("user-token", localStorage.getItem("user-token"));
|
||||
return xhr;
|
||||
}
|
||||
|
||||
function runRenderProcess(params) {
|
||||
document.getElementById("mainContainer").innerHtml = params;
|
||||
}
|
||||
|
||||
function getArticles() {
|
||||
let call = apiCall("/article/get", "GET");
|
||||
call.send();
|
||||
}
|
||||
Reference in New Issue
Block a user