const server = window.location.protocol + "//" + window.location.hostname + ":8000";
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;
}
}
async function register() {
const username = document.getElementById("username").value;
const password = document.getElementById("password").value;
const id = document.getElementById("id").value;
const url = server + "/user/register/"
var body;
if (id == "") {
body = {"name": username, "password": password}
} else {
body = {"name": username, "password": password, "id": id}
}
const req = await xhttp_post(url, body);
if (req.status == 201) {
await login(req.body.id, password)
window.location.href = host + "/user.html"
} 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.html";
} 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 } function transfer() {} function transfer_form() { const to_id = document.getElementById("to_id").value; const color = document.getElementById("color").value; const amount = document.getElementById("amount").value; }