From e48734dfa71fc8016deeaf526c6e716245efb9c8 Mon Sep 17 00:00:00 2001 From: DcruBro Date: Mon, 4 May 2026 14:30:54 +0200 Subject: [PATCH] login in registracija --- login.html | 40 +++++++++++++++++++++++ register.html | 48 ++++++++++++++++++++++++++++ scripts/login.js | 45 ++++++++++++++++++++++++++ scripts/main.js | 29 ++++++++++++++++- scripts/register.js | 78 +++++++++++++++++++++++++++++++++++++++++++++ styles/main.css | 72 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 login.html create mode 100644 register.html create mode 100644 scripts/login.js create mode 100644 scripts/register.js diff --git a/login.html b/login.html new file mode 100644 index 0000000..f9dd8fb --- /dev/null +++ b/login.html @@ -0,0 +1,40 @@ + + + + + + Prijava + + + + + + +
+
+

Prijava

+
+ +
+ + + + + + + \ No newline at end of file diff --git a/register.html b/register.html new file mode 100644 index 0000000..4b0af41 --- /dev/null +++ b/register.html @@ -0,0 +1,48 @@ + + + + + + Registracija + + + + + + +
+
+

Registracija

+
+ +
+ + + + + + + \ No newline at end of file diff --git a/scripts/login.js b/scripts/login.js new file mode 100644 index 0000000..c8fcf2e --- /dev/null +++ b/scripts/login.js @@ -0,0 +1,45 @@ +const errmsg = ` +
+

Napaka pri prijavi! Preverite uporabniško ime in geslo oz. poskusite kasneje.

+
+`; + +document.getElementById("login-form").addEventListener("submit", async function(event) { + event.preventDefault(); + + // Izbriši prejšnje napake + const previousErrors = document.querySelectorAll(".page-paragraph-error"); + previousErrors.forEach(error => error.remove()); + + const username = document.getElementById("username").value; + const password = document.getElementById("password").value; + const form = document.getElementById("login-form"); + + try { + const response = await fetch("http://localhost:3000/prijava", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ "username": username, "password": password }) + }); + + const data = await response.json().catch(() => ({})); + + if (response.status === 200) { + // Nastavi cookie, 3 ure veljaven + document.cookie = `token=${data.token}; max-age=${3 * 60 * 60}; path=/; secure; samesite=strict`; + + window.location.href = "/"; + return; + } + + const message = data.message || "Napaka pri prijavi! Preverite uporabniško ime in geslo oz. poskusite kasneje."; + form.insertAdjacentHTML( + "beforeend", + `

${message}

` + ); + } catch (error) { + form.insertAdjacentHTML("beforeend", errmsg); + } +}); \ No newline at end of file diff --git a/scripts/main.js b/scripts/main.js index 8ac7b2d..2e6e455 100644 --- a/scripts/main.js +++ b/scripts/main.js @@ -8,6 +8,7 @@ const headerHtml = ` O projektu Kontakt Dodatki +