From 0d7ecb65283e8a42d73235d7955b6fe960ca0893 Mon Sep 17 00:00:00 2001 From: IM23a-spirgif Date: Thu, 17 Apr 2025 13:21:17 +0200 Subject: [PATCH 1/4] Formatting + Added Home Page --- gambling.js | 2 +- public/{frontend.js => frontend_login.js} | 5 ++- public/home.html | 16 +++++++ public/login.html | 4 +- public/nogger.html | 10 ----- public/styles_home.css | 51 +++++++++++++++++++++++ public/{styles.css => styles_login.css} | 0 7 files changed, 73 insertions(+), 15 deletions(-) rename public/{frontend.js => frontend_login.js} (91%) create mode 100644 public/home.html delete mode 100644 public/nogger.html create mode 100644 public/styles_home.css rename public/{styles.css => styles_login.css} (100%) diff --git a/gambling.js b/gambling.js index cccde04..72b4f5a 100644 --- a/gambling.js +++ b/gambling.js @@ -105,7 +105,7 @@ app.get("/", authenticateToken, (request, response) => { /* #swagger.security = [{ "bearerAuth": [] }] */ - response.sendFile(path.join(__dirname, "structure.png")); + response.sendFile(path.join(__dirname, ".\\public\\home.html")); }); app.post("/balance", authenticateToken, (request, response) => { diff --git a/public/frontend.js b/public/frontend_login.js similarity index 91% rename from public/frontend.js rename to public/frontend_login.js index 1dfdad5..5382493 100644 --- a/public/frontend.js +++ b/public/frontend_login.js @@ -1,5 +1,4 @@ -const TokenHeader = new Headers(); -document.addEventListener("DOMContentLoaded", () => { +document.addEventListener("DOMContentLoaded", () => { const form = document.querySelector("form"); form.addEventListener("submit", async (e) => { @@ -20,6 +19,7 @@ document.addEventListener("DOMContentLoaded", () => { const loginData = await loginRes.json(); alert(`Welcome back, ${loginData.username}!`); document.cookie = `authorization=${loginData.token}` + window.location.href = "/"; return; } @@ -33,6 +33,7 @@ document.addEventListener("DOMContentLoaded", () => { const registerData = await registerRes.json(); alert(`Account created! Welcome, ${registerData.username}`); document.cookie = `authorization=${registerData.token}` + window.location.href = "/"; } else { const error = await registerRes.json(); alert("Register failed: " + (error.error || "Unknown error")); diff --git a/public/home.html b/public/home.html new file mode 100644 index 0000000..52eb118 --- /dev/null +++ b/public/home.html @@ -0,0 +1,16 @@ + + + + + Home + + + + + + + \ No newline at end of file diff --git a/public/login.html b/public/login.html index 704cd86..94bf5b2 100644 --- a/public/login.html +++ b/public/login.html @@ -3,7 +3,7 @@ Login - +
@@ -12,5 +12,5 @@
- + \ No newline at end of file diff --git a/public/nogger.html b/public/nogger.html deleted file mode 100644 index 0f87b5a..0000000 --- a/public/nogger.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - I am a Nogger Glace - - - - - \ No newline at end of file diff --git a/public/styles_home.css b/public/styles_home.css new file mode 100644 index 0000000..5c5f0be --- /dev/null +++ b/public/styles_home.css @@ -0,0 +1,51 @@ +body { + background: radial-gradient(circle at top, #111, #000); + color: #0f0; + font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; + margin: 0; + height: 100vh; + position: relative; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; +} + +body::before { + content: "♠ ♥ Welcome to Gambling API ♦ ♣"; + font-size: 24px; + color: #0f0; + margin-bottom: 40px; + display: block; + text-shadow: 0 0 5px #0f0, 0 0 10px #0f0; +} + +button { + background-color: #0f0; + color: #000; + border: none; + padding: 15px 30px; + font-size: 16px; + font-weight: bold; + border-radius: 12px; + cursor: pointer; + box-shadow: 0 0 10px #0f0, 0 0 5px #0f0 inset; + transition: background 0.2s ease-in-out, transform 0.2s; +} + +button:hover { + background-color: #1f1; + transform: scale(1.05); +} + +button.logout { + position: absolute; + top: 20px; + right: 20px; + padding: 10px 20px; + font-size: 14px; +} + +button.balance { + margin-top: 20px; +} \ No newline at end of file diff --git a/public/styles.css b/public/styles_login.css similarity index 100% rename from public/styles.css rename to public/styles_login.css From a2d66047ab9ca06a3b4fb0763234fd714e1e069e Mon Sep 17 00:00:00 2001 From: ScriptSorcerer20 Date: Thu, 17 Apr 2025 13:40:06 +0200 Subject: [PATCH 2/4] made logout and lowercase unsensetive login --- data.json | 21 +++++++++++++++++++-- gambling.js | 24 ++++++++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/data.json b/data.json index 41b42e6..80c3487 100644 --- a/data.json +++ b/data.json @@ -1,3 +1,20 @@ [ - -] + { + "username": "jerome", + "password": "Sigma", + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb3dlcl91c2VybmFtZSI6Implcm9tZSIsImlhdCI6MTc0NDg4OTY3MSwiZXhwIjoxNzQ0ODkxNDcxfQ.zYg3uxh_iNZ2xdEma8p3p-URT3Xa7wnYiQyOx0-QTac", + "money": 200 + }, + { + "username": "Gianimon", + "password": "gianimon", + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkdpYW5pbW9uIiwiaWF0IjoxNzQ0ODc0NTczLCJleHAiOjE3NDQ4NzYzNzN9.dGp5WTKIDVhomKZ8J7nevz8O7PqwRhR9wEgeyDtL_XE", + "money": 200 + }, + { + "username": "any", + "password": "any", + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFueSIsImlhdCI6MTc0NDg4OTk1OSwiZXhwIjoxNzQ0ODkxNzU5fQ.TA6BA09DQlmUqEzQxD_wF0r-VyqCMccoxq8SxPnB9-4", + "money": 200 + } +] \ No newline at end of file diff --git a/gambling.js b/gambling.js index cccde04..3c6a661 100644 --- a/gambling.js +++ b/gambling.js @@ -62,7 +62,9 @@ function authenticateToken(request, response, next) { } app.post("/register", (request, response) => { - const { username, password } = request.body; + let { username, password } = request.body; + username = username.toLowerCase(); + if (!username && !password) return response.status(400).json({ error: "Username and password are required" }); @@ -71,18 +73,18 @@ app.post("/register", (request, response) => { return response.status(400).json({ error: "Username already exists" }); } - const token = generateAccessToken({ username }); saveUser({ username, password, token, "money": 200 }); - response.json({ username, token }); + response.json({ lower_username, token }); }); app.get("/login", (request, response) => { response.sendFile(path.join(__dirname, ".\\public\\login.html")); }) app.post("/login", (request, response) => { - const { username, password } = request.body; + let { username, password } = request.body; + username = username.toLowerCase(); if (!username && !password) return response.status(400).json({ error: "Username and password are required" }); @@ -129,13 +131,27 @@ app.post("/verify", authenticateToken, (request, response) => { }); app.delete("/logout", authenticateToken, (request, response) => { + /* #swagger.security = [{ + "bearerAuth": [] + }] */ const username = request.user.username; const users = get_data(); const user = users.find(u => u.username === username); + + // 1) Clear the HttpOnly cookie in the browser + response.clearCookie("token", { + httpOnly: true, + secure: process.env.NODE_ENV === "production", + sameSite: "Strict" + }); + + // 2) Delete the token from your JSON store if (user) { delete user.token; save_data(users); } + + // 3) Send final response response.json({ message: "Logged out successfully." }); }); From 38ae2eb785bc61b7758f851cb14a5dbe97eaf13c Mon Sep 17 00:00:00 2001 From: IM23a-spirgif Date: Thu, 17 Apr 2025 13:56:44 +0200 Subject: [PATCH 3/4] Formatting + Added Home Page --- gambling.js | 6 +++--- public/frontend_home.js | 19 +++++++++++++++++++ public/home.html | 12 ++++++------ public/styles_home.css | 4 +++- 4 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 public/frontend_home.js diff --git a/gambling.js b/gambling.js index 72b4f5a..8c7f01b 100644 --- a/gambling.js +++ b/gambling.js @@ -108,12 +108,12 @@ app.get("/", authenticateToken, (request, response) => { response.sendFile(path.join(__dirname, ".\\public\\home.html")); }); -app.post("/balance", authenticateToken, (request, response) => { +app.get("/balance", authenticateToken, (request, response) => { /* #swagger.security = [{ "bearerAuth": [] }] */ const username = request.user.username; - + console.log(username); const allUsers = get_data(); const me = allUsers.find(u => u.username === username); @@ -121,7 +121,7 @@ app.post("/balance", authenticateToken, (request, response) => { return response.status(404).json({ error: "User not found" }); } - response.json({ balance: me.money }); + response.json({ balance: me.money }).status(200); }); app.post("/verify", authenticateToken, (request, response) => { diff --git a/public/frontend_home.js b/public/frontend_home.js new file mode 100644 index 0000000..6b5c1af --- /dev/null +++ b/public/frontend_home.js @@ -0,0 +1,19 @@ +document.addEventListener("DOMContentLoaded", async () => { + const logoutButton = document.getElementById("logout"); + const balance = document.getElementById("balance"); + try { + const balanceRes = await fetch('/balance', { + method: "GET", + headers: { + "Content-Type": "application/json", + } + }); + if (balanceRes.ok) { + const balanceData = await balanceRes.json(); + console.log(balanceData); + balance.innerHTML = balanceData.balance; + } + } catch (error) { + console.log(error); + } +}); \ No newline at end of file diff --git a/public/home.html b/public/home.html index 52eb118..2fa4810 100644 --- a/public/home.html +++ b/public/home.html @@ -4,13 +4,13 @@ Home + - - + + +
+ Balance: $0 +
\ No newline at end of file diff --git a/public/styles_home.css b/public/styles_home.css index 5c5f0be..ca5ffd6 100644 --- a/public/styles_home.css +++ b/public/styles_home.css @@ -46,6 +46,8 @@ button.logout { font-size: 14px; } -button.balance { +.balance { margin-top: 20px; + color: #0f0; + text-shadow: 0 0 5px #0f0, 0 0 10px #0f0; } \ No newline at end of file From 282ff18155f594e01ec272c5993d631338b4cb94 Mon Sep 17 00:00:00 2001 From: ScriptSorcerer20 Date: Thu, 17 Apr 2025 14:00:07 +0200 Subject: [PATCH 4/4] made logout and lowercase unsensetive login and minor fix --- gambling.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gambling.js b/gambling.js index 3c6a661..f14e171 100644 --- a/gambling.js +++ b/gambling.js @@ -65,7 +65,7 @@ app.post("/register", (request, response) => { let { username, password } = request.body; username = username.toLowerCase(); - if (!username && !password) + if (!username || !password) return response.status(400).json({ error: "Username and password are required" }); const users = get_data(); @@ -85,7 +85,7 @@ app.get("/login", (request, response) => { app.post("/login", (request, response) => { let { username, password } = request.body; username = username.toLowerCase(); - if (!username && !password) + if (!username || !password) return response.status(400).json({ error: "Username and password are required" }); const users = get_data();