1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
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 = {"name": username, "password": password, "id": id};
if (id == "") {
body = {"name": username, "password": password}
}
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 <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function get_session() {
return getCookie("session")
}
function get_id() {
return getCookie("id")
}
function set_session(session, id) {
document.cookie = "session=" + session + "; SameSite=Strict; path=/;";
document.cookie = "id=" + id + "; SameSite=Strict; path=/;";
}
function del_session() {
document.cookie = "session=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
document.cookie = "id=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}
async function logout() {
const id = get_id();
const session = get_session();
const url = server + "/user/logout"
const body = {"id": id, "session": session};
const req = await xhttp_post(url, body);
if (req.status == 200) {
del_session()
window.location.href = host
} else {
alert("Unauthorized")
}
}
async function is_logged_in() {
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) {
return true
} else if (req.status == 404) {
if (req.body == "Not Authenticated") {
del_session()
return false
}
}
} else {
del_session()
}
}
return false
}
async function user() {
const session = get_session();
const id = get_id();
const user_name = document.getElementById("user.name");
const user_id = document.getElementById("user.id");
const users = document.getElementById("users");
const balance = document.getElementById("balance");
if (is_logged_in()) {
const url = server + "/user/get"
const body = {"id": id, "session": session}
const req = await xhttp_post(url, body);
if (req.status == 200) {
const tokens = format_tokovec(req.body.tokovec);
const user_list = await userlist();
user_name.innerHTML = `Name: ${req.body.name}`;
user_id.innerHTML = `ID: ${id}`;
users.innerHTML += user_list;
balance.innerHTML += tokens;
} else if (req.status == 404) {
if (req.body == "Not Authenticated") {
del_session()
}
}
}
}
function format_tokovec(tokovec) {
var out = ""
for (t in tokovec) {
out += `
<p>${tokovec[t].color}: ${tokovec[t].amount}</p>
`
}
return out
}
async function userlist() {
const url = server + "/user/list"
const req = await xhttp_get(url);
var out = ""
try {
if (req.status == 200) {
for (u in req.body) {
const user = req.body[u];
out += `<p>${user.id} -- ${user.name}`
}
} else {
return "<p>User list not available</p>"
}
} 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)
}
}
window.onload = async function() {
// if not on a user page
if (window.location.pathname.indexOf("/user") == -1) {
console.log("NOT A USER PAGE")
// 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()
}
}
async function delete_form() {
const id = get_id();
const session = get_session();
const is_sure = document.getElementById("is_sure").value;
const password = document.getElementById("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!")
}
}
|