const server = window.location.protocol + "//" + window.location.hostname + ":9050";
const host = window.location.protocol + "//" + window.location.host;
const client_id = "POKO Web 0.0"
async function xhttp_get(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
try {
return new Response(response.status, await response.clone().json())
} catch {
return new Response(response.status, await response.text())
}
} catch (error) {
console.error(error.message);
}
}
async function xhttp_post(url, body) {
try {
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
},
});
try {
return new Response(response.status, await response.clone().json())
} catch {
return new Response(response.status, await response.text())
}
} catch (error) {
console.error(error.message)
}
}
class Response {
constructor(status, body) {
this.status = status;
this.body = body;
}
is_ok() {
if (this.status == 200) {
return true
}
return false
}
}
async function register() {
const username = document.getElementById("username").value;
const password = document.getElementById("password").value;
const id = document.getElementById("id").value;
const key = document.getElementById("key").value;
const url = server + "/user/register/"
var body = {"name": username, "password": password, "id": id, "key": key};
if (id == "") {
body = {"name": username, "password": password, "key": key}
}
const req = await xhttp_post(url, body);
if (req.status == 201) {
await login(req.body.id, password)
window.location.href = host + "/user/"
} else {
alert(req.status + ": " + req.body)
}
}
function password_validator() {
const password = document.getElementById("password");
}
function login_form() {
const id = document.getElementById("id").value;
const password = document.getElementById("password").value;
login(id, password)
}
async function login(id, password) {
const url = server + "/user/login"
const body = {"id": id, "password": password, "clientid": client_id};
const req = await xhttp_post(url, body);
if (req.status == 200) {
// Set session cookie
set_session(req.body, id)
window.location.href = host + "/user/";
} else {
alert(req.status + ": " + req.body)
}
}
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i
${user.id} -- ${user.name}` } } else { return "
User list not available
" } } catch {} return out } async function transfer(to_id, color, amount) { const id = get_id(); const session = get_session(); if (is_logged_in()) { const url = server + "/transfer/out"; const body = {"id": id, "session": session, "to": to_id, "color": color, "amount": Number(amount)}; const req = await xhttp_post(url, body); if (req.status != 200) { alert(req.status + ":" + req.body) } else if (req.status == 200) { window.location.href = host + "/user/" } } } function transfer_form() { const to_id = document.getElementById("to_id").value; const color_raw = document.getElementById("color").value.toLowerCase(); const color = color_raw.substr(0, 1).toUpperCase() + color_raw.slice(1) const amount = document.getElementById("amount").value; console.log(to_id, color, amount) transfer(to_id, color, amount) } async function update_form_onload() { const name = document.getElementById("name"); const id = get_id(); const session = get_session(); const url = server + "/user/get" const body = {"id": id, "session": session} const req = await xhttp_post(url, body); if (req.status == 200) { name.value = req.body.name; } } async function update_password_form() { const id = get_id(); const session = get_session(); const old_password = document.getElementById("old_password").value; const new_password = document.getElementById("new_password").value; const url = server + "/user/update/password" const body = {"id": id, "session": session, "old_password": old_password, "new_password": new_password}; const req = await xhttp_post(url, body); if (req.status == 200) { window.location.href = host + "/user/" } else { alert(req.status + ": " + req.body) } } async function update_name_form() { const id = get_id(); const session = get_session(); const name = document.getElementById("name").value; const url = server + "/user/update/name" const body = {"id": id, "session": session, "name": name}; const req = await xhttp_post(url, body); if (req.status == 200) { window.location.href = host + "/user/" } else { alert(req.status + ": " + req.body) } } async function is_admin() { const session = get_session(); const id = get_id(); if (session != "") { if (id != "") { const url = server + "/user/get" const body = {"id": id, "session": session} const req = await xhttp_post(url, body); if (req.status == 200) { if (req.body.is_admin == true) { return true } } else if (req.status == 404) { if (req.body == "Not Authenticated") { del_session() return false } } } else { del_session() } } return false } window.onload = async function() { // if not on a user page and not an admin page if (window.location.pathname.indexOf("/user") == -1 && window.location.pathname.indexOf("/admin") == -1) { // and if logged in if (await is_logged_in()) { // move to the user index page window.location.href = host + "/user/" } } else if (window.location.pathname == "/user/") { await user() } else if (window.location.pathname == "/admin/regkeys/") { await regkeys() } else if (window.location.pathname == "/user/update/") { await update_form_onload() } if (await is_admin()) { const elements = document.getElementsByTagName("header"); for (x in elements) { elements[x].innerHTML += `${req.body[x]}
` } } else { list.innerHTML = req.body } } async function add_regkey_form() { const id = get_id(); const session = get_session(); const key = document.getElementById("key_add").value; const url = server + "/admin/regkey/new" const body = {"id": id, "session": session, "key": key} const req = await xhttp_post(url, body); if (req.status == 200) { window.location.href = window.location.href } else { alert(req.status + ": " + req.body) } } async function del_regkey_form() { const id = get_id(); const session = get_session(); const key = document.getElementById("key_del").value; const url = server + "/admin/regkey/del" const body = {"id": id, "session": session, "key": key} const req = await xhttp_post(url, body); if (req.status == 200) { window.location.href = window.location.href } else { alert(req.status + ": " + req.body) } } async function delete_form() { const id = get_id(); const session = get_session(); const is_sure = document.getElementById("is_sure").value; const password = document.getElementById("del_password").value; if (is_sure == "YES") { const url = server + "/user/delete" const body = {"id": id, "session": session, "password": password} const req = await xhttp_post(url, body); if (req.status == 200) { del_session() window.location.href = host } else { alert("Could not delete user!") } } else { alert("You have to be sure to delete your user!") } }