This commit is contained in:
CodingTurtle2 committed 2025-12-21 18:18:12 +01:00
1 parent 7e22029791
commit ff68e8d9b9
3 files changed
+141 -57

No files matched your search

+2
View File
@@ -2,3 +2,5 @@ const couchdb_username = "admin";
const couchdb_password = "admin"; const couchdb_password = "admin";
const couchdb_name = "sassy"; const couchdb_name = "sassy";
//http://127.0.0.1:5984/_utils/#_config/couchdb@127.0.0.1
+83 -57
View File
@@ -18,8 +18,12 @@ function _onOverlayDestroy(elm) {
const ori_elm = getId(elm.getAttribute("name")); const ori_elm = getId(elm.getAttribute("name"));
if (ori_elm) { if (ori_elm) {
const cb = ori_elm.getAttribute("onclose"); const cb = ori_elm.getAttribute("onclose");
if (typeof(cb) == "string") { if (typeof cb == "string") {
try { eval(cb); } catch(err) { console.error(err); } try {
eval(cb);
} catch (err) {
console.error(err);
}
} }
} }
elm.remove(); elm.remove();
@@ -29,7 +33,6 @@ function hideOverlay(id) {
for (var i of elms) _onOverlayDestroy(i); for (var i of elms) _onOverlayDestroy(i);
} }
function _main_events() { function _main_events() {
const header_bar_1 = document.querySelector(".header_bar_1"); const header_bar_1 = document.querySelector(".header_bar_1");
if (header_bar_1 != null) { if (header_bar_1 != null) {
@@ -41,7 +44,7 @@ function _main_events() {
isFloating = true; isFloating = true;
} }
}); });
window.addEventListener("scroll", function() { window.addEventListener("scroll", function () {
if (window.pageYOffset > 0 && !isFloating) { if (window.pageYOffset > 0 && !isFloating) {
header_bar_1.style.animationDuration = "0.5s"; header_bar_1.style.animationDuration = "0.5s";
header_bar_1.classList.remove("header_bar_1_docked"); header_bar_1.classList.remove("header_bar_1_docked");
@@ -58,35 +61,36 @@ function _main_events() {
} }
_main_events(); _main_events();
/* ----------- couch db ----------- */ /* ----------- couch db ----------- */
//http://127.0.0.1:5984/_utils/#_config/couchdb@127.0.0.1 //http://127.0.0.1:5984/_utils/#_config/couchdb@127.0.0.1
class CouchDB { class CouchDB {
static _default_url = "http://127.0.0.1:5984/"; static _default_url = "http://127.0.0.1:5984/";
constructor() { constructor() {
this._does_auth_exists_err(); this._does_auth_exists_err();
this.request = new XMLHttpRequest(); this.request = new XMLHttpRequest();
this.db_name = couchdb_name; this.db_name = couchdb_name;
this.is_online = 2; this.is_online = 2;
} }
//private methods //private methods
_does_auth_exists() { _does_auth_exists() {
return ( return (
typeof(couchdb_username) == "string" && typeof couchdb_username == "string" &&
typeof(couchdb_password) == "string" && typeof couchdb_password == "string" &&
typeof(couchdb_name) == "string" typeof couchdb_name == "string"
); );
} }
_does_auth_exists_err() { _does_auth_exists_err() {
if (!this._does_auth_exists()) throw 'Keine Auth vorhanden. Digga, mach mal <script src="./auth.js"></script>'; if (!this._does_auth_exists())
throw 'Keine Auth vorhanden. Digga, mach mal <script src="./auth.js"></script>';
} }
_is_error_missing_database(errReason) { _is_error_missing_database(errReason) {
return errReason === "no_db_file" || errReason === "Database does not exist."; return (
errReason === "no_db_file" || errReason === "Database does not exist."
);
} }
_is_error_missing_data(errReason) { _is_error_missing_data(errReason) {
return errReason === "missing"; return errReason === "missing";
@@ -94,15 +98,15 @@ class CouchDB {
async _ping() { async _ping() {
try { try {
const response = await fetch(CouchDB._default_url, { const response = await fetch(CouchDB._default_url, {
method: "GET" method: "GET",
}); });
return response.ok; return response.ok;
} catch(err) { } catch (err) {
return false; return false;
} }
} }
_send_db_request(method, url, body = null, errorHandling = true) { _send_db_request(method, url, body = null, errorHandling = true) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
if (this.is_online == 2) { if (this.is_online == 2) {
@@ -112,13 +116,13 @@ class CouchDB {
reject("CouchDB is not online!"); reject("CouchDB is not online!");
return; return;
} }
const dburl = CouchDB._default_url + this.db_name + "/" + url; const dburl = CouchDB._default_url + this.db_name + "/" + url;
const request = this.request; const request = this.request;
this.request.onreadystatechange = () => { this.request.onreadystatechange = () => {
if (request.readyState != 4) return; if (request.readyState != 4) return;
try { try {
if (request.status == 200 || request.status == 201) { if (request.status == 200 || request.status == 201) {
var response = JSON.parse(request.responseText); var response = JSON.parse(request.responseText);
@@ -129,54 +133,66 @@ class CouchDB {
var json = JSON.parse(request.responseText); var json = JSON.parse(request.responseText);
if (this._is_error_missing_data(json.reason)) { if (this._is_error_missing_data(json.reason)) {
resolve(null); resolve(null);
} else if (errorHandling && this._is_error_missing_database(json.reason)) { } else if (
this.createDB().then((value) => { errorHandling &&
this._send_db_request(method, url, body, false).then((value) => { this._is_error_missing_database(json.reason)
resolve(value); ) {
}).catch((err) => { this.createDB()
.then((value) => {
this._send_db_request(method, url, body, false)
.then((value) => {
resolve(value);
})
.catch((err) => {
reject(err);
});
})
.catch((err) => {
reject(err); reject(err);
}); });
}).catch((err) => {
reject(err);
});
} else { } else {
reject(`Database not found: ${request.status} (Reason: ${reason.json})`); reject(
`Database not found: ${request.status} (Reason: ${reason.json})`,
);
} }
} else { } else {
reject(`Unexpected status: ${request.status} (Response: ${ reject(
request.responseText `Unexpected status: ${request.status} (Response: ${
})`); request.responseText
})`,
);
} }
} catch(err) { } catch (err) {
reject("Unexpected error: " + err); reject("Unexpected error: " + err);
} }
}; };
this.request.open(method, dburl, true); this.request.open(method, dburl, true);
this.request.setRequestHeader("Authorization", "Basic " + btoa( this.request.setRequestHeader(
couchdb_username + ":" + couchdb_password "Authorization",
)); "Basic " + btoa(couchdb_username + ":" + couchdb_password),
);
this.request.setRequestHeader("Content-type", "application/json"); this.request.setRequestHeader("Content-type", "application/json");
this.request.send(body); this.request.send(body);
}); });
} }
async _get_db_data(id) { async _get_db_data(id) {
return await this._send_db_request("GET", id); return await this._send_db_request("GET", id);
} }
//public methods //public methods
async checkOnlineStatus() { async checkOnlineStatus() {
const res = await this._ping(); const res = await this._ping();
if (res === true) { if (res === true) {
this.is_online = 1; this.is_online = 1;
} else { } else {
this.is_online = 0; this.is_online = 0;
} }
return res; return res;
} }
async createDB() { async createDB() {
@@ -184,47 +200,57 @@ class CouchDB {
} }
async get(id) { async get(id) {
const res = await this._get_db_data(id); const res = await this._get_db_data(id);
if (res == null) return null;
delete res["_id"]; delete res["_id"];
delete res["_rev"]; delete res["_rev"];
return res; return res;
} }
async set(id, content, replace = true) { async set(id, content, replace = true) {
try { try {
if (typeof (content) != "object") throw "basic.js - CouchDB->set: content is not from type object (type = " + typeof(content) + ")"; if (typeof content != "object")
throw (
"basic.js - CouchDB->set: content is not from type object (type = " +
typeof content +
")"
);
let existingDoc; let existingDoc;
try { try {
existingDoc = await this._get_db_data(id); existingDoc = await this._get_db_data(id);
} catch (err) { } catch (err) {
existingDoc = null; existingDoc = null;
} }
if (existingDoc) { if (existingDoc) {
if (replace) { if (replace) {
content = { content = {
...content, ...content,
_id: existingDoc._id, _id: existingDoc._id,
_rev: existingDoc._rev _rev: existingDoc._rev,
}; };
} else { } else {
content = { content = {
...existingDoc, ...existingDoc,
...content, ...content,
_rev: existingDoc._rev _rev: existingDoc._rev,
}; };
} }
} }
const res = await this._send_db_request("PUT", id, JSON.stringify(content)); const res = await this._send_db_request(
"PUT",
id,
JSON.stringify(content),
);
return { return {
"type": "success", type: "success",
"message": res message: res,
}; };
} catch(err) { } catch (err) {
console.error(err); console.error(err);
return { return {
"type": "error", type: "error",
"message": err message: err,
}; };
} }
} }
} }
+56
View File
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="./lib/basic.css">
<script src="./auth.js"></script>
<script src="./lib/basic.js"></script>
</head>
<body>
<input id="username"><br>
<input type="password" id="password">
<button onclick="login()">Submit</button>
<script>
const couchDB = new CouchDB();
async function getData() {
let res = await couchDB.get("users");
if (!res) res = {};
return res;
}
async function login() {
const data = await getData();
const username = getId("username").value;
const password = getId("password").value;
const user = data[username];
//{"turtle":{"password": "password","level":1}}
if (user == undefined) {
alert("Falscher Nutzername!");
return;
}
if (user.password != password) {
alert("Falsches Passwort!");
return;
}
location.href = "/uebersicht.html";
}
couchDB.set("users", {
turtle: {
password: "password",
level: 1,
isProf: true
}
});
</script>
</body>
</html>