Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled
97 lines
3.4 KiB
JavaScript
97 lines
3.4 KiB
JavaScript
const loginform = document.querySelector(".loginform");
|
|
const loginformbody = document.querySelector(".loginformbody");
|
|
const loginusername = document.querySelector("#loginusernameinput");
|
|
const loginpassword = document.querySelector("#loginpasswordinput");
|
|
const exitbutton = document.querySelector("#exitbutton");
|
|
const adminpanelsection = document.querySelector(".adminpanel");
|
|
const headerusername = document.querySelector(".headerusername");
|
|
|
|
const hashPassword = async (password) => {
|
|
const encoder = new TextEncoder();
|
|
const data = encoder.encode(password);
|
|
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
const hashHex = hashArray
|
|
.map((b) => ("00" + b.toString(16)).slice(-2))
|
|
.join("");
|
|
return hashHex;
|
|
};
|
|
|
|
if (localStorage.getItem("token") === null) {
|
|
adminpanelsection.classList.remove("active");
|
|
loginform.classList.add("active");
|
|
} else {
|
|
adminpanelsection.classList.add("active");
|
|
infosection.classList.add("active");
|
|
headerusername.innerHTML = JSON.parse(
|
|
localStorage.getItem("userdata"),
|
|
).username;
|
|
}
|
|
|
|
loginformbody.addEventListener("submit", (e) => {
|
|
e.preventDefault();
|
|
hashPassword(loginpassword.value).then((hashedpassword) => {
|
|
login(loginusername.value, hashedpassword);
|
|
});
|
|
});
|
|
|
|
const login = async (username, password) => {
|
|
const response = await fetch(routes.login(), {
|
|
method: "POST",
|
|
body: `{"username":"${username}","password":"${password}"}`,
|
|
})
|
|
.then((data) => data.json())
|
|
.then((jsondata) => jsondata)
|
|
.catch((error) => {
|
|
if (error.message === "Unexpected end of JSON input") {
|
|
notification(
|
|
`Ошибка на сервере: Создайте файл users.json и запишите в него первого пользователя`,
|
|
"error",
|
|
);
|
|
} else {
|
|
notification(`Ошибка на сервере: ${error}`, "error");
|
|
}
|
|
});
|
|
if (response.status === "403") {
|
|
notification("Неправильный логин или пароль", "error");
|
|
}
|
|
if (response.token) {
|
|
notification("Успешно", "success");
|
|
loginform.classList.remove("active");
|
|
localStorage.setItem("token", response.token);
|
|
await get_all_settings();
|
|
adminpanelsection.classList.add("active");
|
|
await get_user_data();
|
|
loginusername.value = "";
|
|
loginpassword.value = "";
|
|
}
|
|
};
|
|
|
|
const get_user_data = async () => {
|
|
const response = await fetch(routes.user(), {
|
|
method: "POST",
|
|
body: `{"token":"${localStorage.getItem("token")}"}`,
|
|
})
|
|
.then((data) => data.json())
|
|
.then((jsondata) => jsondata)
|
|
.catch((error) => {
|
|
notification(`Ошибка на сервере: ${error}`, "error");
|
|
});
|
|
if (response.id) {
|
|
headerusername.innerHTML = response.username;
|
|
localStorage.setItem("userdata", JSON.stringify(response));
|
|
}
|
|
};
|
|
|
|
const logout = () => {
|
|
localStorage.removeItem('userdata');
|
|
localStorage.removeItem('token')
|
|
loginform.classList.add("active");
|
|
notification("Вы вышли", "success");
|
|
adminpanelsection.classList.remove("active");
|
|
};
|
|
|
|
exitbutton.addEventListener("click", () => {
|
|
logout();
|
|
});
|