From 62e0487e8e8301dde5567847ef17c0c3bb9ff9e1 Mon Sep 17 00:00:00 2001 From: Ianyourgod Date: Wed, 6 Mar 2024 12:15:38 -0600 Subject: [PATCH] aghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhyyyyyyyyyyyyyyyyyyyyyygoiubklfdjgxnrvsdkjfgbhnuuuuuuuuuuuuuuuuugijk --- api/db/UserManager.js | 5 +--- api/v1/routes/users/assignPossition.js | 4 +-- api/v1/routes/users/createAccount.js | 7 +++-- index.js | 19 ++++++++++-- package.json | 3 +- reset.js | 12 ++++++++ test.html | 40 ++++++++++++++++++++++++++ 7 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 reset.js create mode 100644 test.html diff --git a/api/db/UserManager.js b/api/db/UserManager.js index 0cf5e94..de7c9ac 100644 --- a/api/db/UserManager.js +++ b/api/db/UserManager.js @@ -66,12 +66,9 @@ class UserManager { async reset(understands = false) { if (!understands) { let unde = prompt("This deletes ALL DATA. Are you sure? (Y/n) ") - if ( - typeof unde !== "string" - ) { + if (typeof unde !== "string") { return; } - console.log(typeof unde) } await this.users.deleteMany({}); await this.reports.deleteMany({}); diff --git a/api/v1/routes/users/assignPossition.js b/api/v1/routes/users/assignPossition.js index 71092df..9764ca1 100644 --- a/api/v1/routes/users/assignPossition.js +++ b/api/v1/routes/users/assignPossition.js @@ -1,6 +1,6 @@ module.exports = (app, utils) => { - app.get('/api/v1/users/assignPossition', async function (req, res) { - const packet = req.query; + app.post('/api/v1/users/assignPossition', async function (req, res) { + const packet = req.body; if (!await utils.UserManager.loginWithToken(packet.user, packet.token)) { utils.error(res, 400, "Reauthenticate"); return; diff --git a/api/v1/routes/users/createAccount.js b/api/v1/routes/users/createAccount.js index 0dbc097..f07640f 100644 --- a/api/v1/routes/users/createAccount.js +++ b/api/v1/routes/users/createAccount.js @@ -1,6 +1,7 @@ module.exports = (app, utils) => { - app.get("/api/v1/users/createAccount", async function (req, res) { - const packet = req.query; + app.post("/api/v1/users/createAccount", async function (req, res) { + const packet = req.body; + if (!packet.username || !packet.password) { utils.error(res, 400, "InvalidData"); return; @@ -17,7 +18,7 @@ module.exports = (app, utils) => { return; } - if (await utils.UserManager.accountExists(packet.username)) { + if (await utils.UserManager.existsByUsername(packet.username)) { utils.error(res, 400, "AccountExists"); return; } diff --git a/index.js b/index.js index 9d2ea7d..315327f 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -const bodyParser = require('body-parser'); require('dotenv').config(); const cors = require('cors'); const rateLimit = require('express-rate-limit'); @@ -16,10 +15,13 @@ app.use(cors({ origin: '*', utilsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 })); -app.use(bodyParser.urlencoded({ +app.use(express.urlencoded({ limit: process.env.ServerSize, extended: false })); +app.use(express.json({ + limit: process.env.ServerSize +})); app.set('trust proxy', 1); app.use(rateLimit({ validate: { @@ -32,19 +34,30 @@ app.use(rateLimit({ legacyHeaders: false, })); +function error(res, code, message) { + res.status(code); + res.header("Content-Type", 'application/json'); + res.json({ "error": message }); +} + const Cast = new cast(); const UserManager = new um(); (async () => { await UserManager.init(); + app.get("/test", (req, res) => { + res.sendFile(path.join(__dirname, 'test.html')); + }); + endpointLoader(app, 'v1/routes', { UserManager: UserManager, homeDir: path.join(__dirname, "./"), Cast: Cast, escapeXML: functions.escapeXML, generateProfileJSON: functions.generateProfileJSON, - safeZipParse: functions.safeZipParse + safeZipParse: functions.safeZipParse, + error: error }); app.listen(PORT, () => { diff --git a/package.json b/package.json index 8a4de71..39e3211 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "prompt-sync": "^4.2.0" }, "scripts": { - "dev": "nodemon index.js" + "dev": "nodemon index.js", + "reset": "node reset.js" } } diff --git a/reset.js b/reset.js new file mode 100644 index 0000000..cc34062 --- /dev/null +++ b/reset.js @@ -0,0 +1,12 @@ +const um = require('./api/db/UserManager'); + +const UserManager = new um(); + +(async () => { + console.log("Initializing database...") + await UserManager.init(); + console.log("Database initialized.") + console.log("Resetting database...") + await UserManager.reset(); + console.log("Database reset complete.") +})(); \ No newline at end of file diff --git a/test.html b/test.html new file mode 100644 index 0000000..f5a7a30 --- /dev/null +++ b/test.html @@ -0,0 +1,40 @@ + + + + + + Document + + + + + + + + + \ No newline at end of file