From 56a8d165ed36c433337a8f8001c8aa89bbeb4f27 Mon Sep 17 00:00:00 2001 From: ScriptSorcerer20 Date: Thu, 17 Apr 2025 10:46:44 +0200 Subject: [PATCH 1/4] Added money --- data.json | 20 ++++++++++++++++++++ gambling.js | 19 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 data.json diff --git a/data.json b/data.json new file mode 100644 index 0000000..366ecfa --- /dev/null +++ b/data.json @@ -0,0 +1,20 @@ +[ + { + "username": "jerome", + "password": "Sigma", + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Implcm9tZSIsImlhdCI6MTc0NDg3NDQ2MCwiZXhwIjoxNzQ0ODc2MjYwfQ.4FnQwoi25uSs8VQWBgC3MRvaZfqcoDN8041fky4YDo4", + "money": 200 + }, + { + "username": "Gianimon", + "password": "gianimon", + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkdpYW5pbW9uIiwiaWF0IjoxNzQ0ODc0NTczLCJleHAiOjE3NDQ4NzYzNzN9.dGp5WTKIDVhomKZ8J7nevz8O7PqwRhR9wEgeyDtL_XE", + "money": 200 + }, + { + "username": "any", + "password": "any", + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFueSIsImlhdCI6MTc0NDg3OTQ1NCwiZXhwIjoxNzQ0ODgxMjU0fQ.xTV0yfMBO0BTsmggjouwYrx180un9rHiDljQqN6IUdw", + "money": 200 + } +] \ No newline at end of file diff --git a/gambling.js b/gambling.js index c442bcd..1351e86 100644 --- a/gambling.js +++ b/gambling.js @@ -69,8 +69,9 @@ app.post("/register", (request, response) => { return response.status(400).json({ error: "Username already exists" }); } + const token = generateAccessToken({ username }); - saveUser({ username, password, token }); + saveUser({ username, password, token, "money": 200 }); response.json({ username, token }); }); @@ -102,6 +103,22 @@ app.get("/", authenticateToken, (request, response) => { response.sendFile(path.join(__dirname, "structure.png")); }); +app.post("/balance", authenticateToken, (request, response) => { + /* #swagger.security = [{ + "bearerAuth": [] + }] */ + const username = request.user.username; + + const allUsers = get_data(); + const me = allUsers.find(u => u.username === username); + + if (!me) { + return response.status(404).json({ error: "User not found" }); + } + + response.json({ balance: me.money }); +}); + app.post("/verify", authenticateToken, (request, response) => { response.json({ valid: true, user: request.user }); }); From 69f48379bd1b3a14bc550d498a6ef3aea63ca6b3 Mon Sep 17 00:00:00 2001 From: IM23a-spirgif Date: Thu, 17 Apr 2025 11:42:37 +0200 Subject: [PATCH 2/4] Added Login + Register Frontend and Authorization --- gambling.js | 13 +++++++---- package.json | 1 + public/frontend.js | 45 ++++++++++++++++++++++++++++++++++++++ public/login.html | 16 ++++++++++++++ public/nogger.html | 10 +++++++++ public/styles.css | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 public/frontend.js create mode 100644 public/login.html create mode 100644 public/nogger.html create mode 100644 public/styles.css diff --git a/gambling.js b/gambling.js index 1351e86..cccde04 100644 --- a/gambling.js +++ b/gambling.js @@ -2,15 +2,17 @@ const express = require("express"); const app = express(); const swaggerUi = require("swagger-ui-express"); const swaggerDocument = require("./swagger.json"); - const path = require("path"); +app.use(express.static(path.join(__dirname, "public"))); const fs = require("fs"); const dotenv = require("dotenv").config(); const jwt = require("jsonwebtoken"); +const cookieParser = require("cookie-parser"); const port = 3000; app.use(express.json()); app.use("/swagger-ui", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); +app.use(cookieParser()); function get_data() { const dataPath = path.join(__dirname, "data.json"); @@ -39,9 +41,9 @@ function authenticateToken(request, response, next) { /* #swagger.security = [{ "bearerAuth": [] }] */ - const authHeader = request.headers.authorization; - console.log(authHeader); - const token = authHeader && authHeader.split(" ")[1]; + const token = + request.headers.authorization?.split(" ")[1] || + request.cookies.authorization; if (token == null) return response.sendStatus(401); @@ -76,6 +78,9 @@ app.post("/register", (request, response) => { response.json({ 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; if (!username && !password) diff --git a/package.json b/package.json index d805e5f..4d40208 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ }, "private": true, "dependencies": { + "cookie-parser": "^1.4.7", "dotenv": "^16.5.0", "express": "^5.1.0", "jsonwebtoken": "^9.0.2", diff --git a/public/frontend.js b/public/frontend.js new file mode 100644 index 0000000..1dfdad5 --- /dev/null +++ b/public/frontend.js @@ -0,0 +1,45 @@ +const TokenHeader = new Headers(); +document.addEventListener("DOMContentLoaded", () => { + const form = document.querySelector("form"); + + form.addEventListener("submit", async (e) => { + e.preventDefault(); + + const username = form.username.value; + const password = form.password.value; + const credentials = {username, password}; + + try { + const loginRes = await fetch("/login", { + method: "POST", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify(credentials), + }); + + if (loginRes.ok) { + const loginData = await loginRes.json(); + alert(`Welcome back, ${loginData.username}!`); + document.cookie = `authorization=${loginData.token}` + return; + } + + const registerRes = await fetch("/register", { + method: "POST", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify(credentials), + }); + + if (registerRes.ok) { + const registerData = await registerRes.json(); + alert(`Account created! Welcome, ${registerData.username}`); + document.cookie = `authorization=${registerData.token}` + } else { + const error = await registerRes.json(); + alert("Register failed: " + (error.error || "Unknown error")); + } + } catch (err) { + console.error("Network error:", err); + alert("Server unreachable."); + } + }); +}); \ No newline at end of file diff --git a/public/login.html b/public/login.html new file mode 100644 index 0000000..704cd86 --- /dev/null +++ b/public/login.html @@ -0,0 +1,16 @@ + + + + + Login + + + +
+ + + +
+ + + \ No newline at end of file diff --git a/public/nogger.html b/public/nogger.html new file mode 100644 index 0000000..0f87b5a --- /dev/null +++ b/public/nogger.html @@ -0,0 +1,10 @@ + + + + + I am a Nogger Glace + + + + + \ No newline at end of file diff --git a/public/styles.css b/public/styles.css new file mode 100644 index 0000000..58483f0 --- /dev/null +++ b/public/styles.css @@ -0,0 +1,54 @@ +body { + background: radial-gradient(circle at top, #111, #000); + color: #fff; + font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + margin: 0; +} + +form { + background: rgba(0, 0, 0, 0.85); + border: 2px solid #0f0; + border-radius: 12px; + padding: 40px 30px; + box-shadow: 0 0 15px rgba(0, 255, 0, 0.3), 0 0 5px rgba(255, 255, 255, 0.1); + display: flex; + flex-direction: column; + gap: 20px; + width: 300px; +} + +input { + padding: 12px 15px; + border: none; + border-radius: 8px; + font-size: 16px; + background: #222; + color: white; + outline: none; + box-shadow: inset 0 0 5px #0f0; + transition: 0.2s ease-in-out; +} + +input:focus { + box-shadow: inset 0 0 10px #0f0, 0 0 5px #0f0; +} + +button { + padding: 12px; + background: #06d429; + color: #000; + border: none; + border-radius: 8px; + font-weight: bold; + cursor: pointer; + transition: background 0.2s ease-in-out; + font-size: 16px; +} + +button:hover { + background: #1f1; +} \ No newline at end of file From fc4937535832971eb3ae57e1ad524a7f3173f51d Mon Sep 17 00:00:00 2001 From: SlideBloodForgeCrown Date: Thu, 17 Apr 2025 12:49:18 +0200 Subject: [PATCH 3/4] Delete swagger.json --- swagger.json | 147 --------------------------------------------------- 1 file changed, 147 deletions(-) delete mode 100644 swagger.json diff --git a/swagger.json b/swagger.json deleted file mode 100644 index f07eb11..0000000 --- a/swagger.json +++ /dev/null @@ -1,147 +0,0 @@ -{ - "openapi": "3.0.0", - "info": { - "title": "GAMBLE!", - "description": "Gambling API", - "version": "1.0.0" - }, - "servers": [ - { - "url": "http://localhost:3000/" - } - ], - "paths": { - "/register": { - "post": { - "description": "", - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request" - } - }, - "requestBody": { - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "username": { - "example": "any" - } - } - } - } - } - } - } - }, - "/login": { - "post": { - "description": "", - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request" - }, - "401": { - "description": "Unauthorized" - } - }, - "requestBody": { - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "username": { - "example": "any" - } - } - } - } - } - } - } - }, - "/": { - "get": { - "description": "", - "parameters": [ - { - "name": "authorization", - "in": "header", - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "401": { - "description": "Unauthorized" - }, - "403": { - "description": "Forbidden" - } - } - } - }, - "/verify": { - "post": { - "description": "", - "parameters": [ - { - "name": "authorization", - "in": "header", - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "401": { - "description": "Unauthorized" - }, - "403": { - "description": "Forbidden" - } - } - } - }, - "/logout": { - "delete": { - "description": "", - "parameters": [ - { - "name": "authorization", - "in": "header", - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "401": { - "description": "Unauthorized" - }, - "403": { - "description": "Forbidden" - } - } - } - } - } -} \ No newline at end of file From e7e225bbe9aee58617c6fd41768a1c01a04df546 Mon Sep 17 00:00:00 2001 From: SlideBloodForgeCrown Date: Thu, 17 Apr 2025 12:49:37 +0200 Subject: [PATCH 4/4] Update data.json --- data.json | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/data.json b/data.json index 366ecfa..41b42e6 100644 --- a/data.json +++ b/data.json @@ -1,20 +1,3 @@ [ - { - "username": "jerome", - "password": "Sigma", - "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Implcm9tZSIsImlhdCI6MTc0NDg3NDQ2MCwiZXhwIjoxNzQ0ODc2MjYwfQ.4FnQwoi25uSs8VQWBgC3MRvaZfqcoDN8041fky4YDo4", - "money": 200 - }, - { - "username": "Gianimon", - "password": "gianimon", - "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkdpYW5pbW9uIiwiaWF0IjoxNzQ0ODc0NTczLCJleHAiOjE3NDQ4NzYzNzN9.dGp5WTKIDVhomKZ8J7nevz8O7PqwRhR9wEgeyDtL_XE", - "money": 200 - }, - { - "username": "any", - "password": "any", - "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFueSIsImlhdCI6MTc0NDg3OTQ1NCwiZXhwIjoxNzQ0ODgxMjU0fQ.xTV0yfMBO0BTsmggjouwYrx180un9rHiDljQqN6IUdw", - "money": 200 - } -] \ No newline at end of file + +]