Merge remote-tracking branch 'origin/main'
This commit is contained in:
+27
-5
@@ -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);
|
||||
|
||||
@@ -69,12 +71,16 @@ 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 });
|
||||
});
|
||||
|
||||
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)
|
||||
@@ -102,6 +108,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 });
|
||||
});
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
},
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"cookie-parser": "^1.4.7",
|
||||
"dotenv": "^16.5.0",
|
||||
"express": "^5.1.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
|
||||
@@ -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.");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Login</title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<form method="post" action="/login">
|
||||
<input type="text" name="username" placeholder="Username">
|
||||
<input type="password" name="password" placeholder="Password">
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
</body>
|
||||
<script src="frontend.js"></script>
|
||||
</html>
|
||||
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>I am a Nogger Glace</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -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;
|
||||
}
|
||||
-147
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user