Skip to content

Commit

Permalink
aghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhyyyyyyyyyyyyyyyyyyyyyygoiubklfdj…
Browse files Browse the repository at this point in the history
…gxnrvsdkjfgbhnuuuuuuuuuuuuuuuuugijk
  • Loading branch information
Ianyourgod committed Mar 6, 2024
1 parent 820ccda commit 62e0487
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 13 deletions.
5 changes: 1 addition & 4 deletions api/db/UserManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down
4 changes: 2 additions & 2 deletions api/v1/routes/users/assignPossition.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 4 additions & 3 deletions api/v1/routes/users/createAccount.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand Down
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const bodyParser = require('body-parser');
require('dotenv').config();
const cors = require('cors');
const rateLimit = require('express-rate-limit');
Expand All @@ -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: {
Expand All @@ -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, () => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"prompt-sync": "^4.2.0"
},
"scripts": {
"dev": "nodemon index.js"
"dev": "nodemon index.js",
"reset": "node reset.js"
}
}
12 changes: 12 additions & 0 deletions reset.js
Original file line number Diff line number Diff line change
@@ -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.")
})();
40 changes: 40 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input id="username" type="text" placeholder="Username">
<input id="password" type="password" placeholder="Password">
<button onclick="createAccount()">Create Account</button>

<script>
function createAccount() {
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;

console.log(username, password);

(async () => {
fetch("/api/v1/users/createAccount", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
username: username,
password: password
})
})
.then(res => res.json())
.then(res => {
console.log(res);
alert(res);
})
})();
}
</script>
</body>
</html>

0 comments on commit 62e0487

Please sign in to comment.