aboutsummaryrefslogtreecommitdiff
path: root/secrets/index.js
diff options
context:
space:
mode:
authorMarvin Borner2021-01-29 21:14:36 +0100
committerMarvin Borner2021-01-29 21:14:36 +0100
commitb5058eb7ff8c110bc24a64d02bc42e0a9dc267f5 (patch)
tree44dc8b82fb9f6f254c001b4800f8efc9654b7847 /secrets/index.js
parentf9ccf6ed47254edf7c1fb6231212bd4b4d6c8356 (diff)
It's no secret that our code is a mess
Diffstat (limited to 'secrets/index.js')
-rw-r--r--secrets/index.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/secrets/index.js b/secrets/index.js
new file mode 100644
index 0000000..22a6f9c
--- /dev/null
+++ b/secrets/index.js
@@ -0,0 +1,45 @@
+const express = require("express");
+const db = require("../db");
+const app = express.Router();
+const { checkUser } = require("../auth");
+
+app.use("/", checkUser, express.static(__dirname + "/public"));
+
+app.post("/api/add", checkUser, async (req, res) => {
+ if (!req.body.secret) return res.send("error");
+ try {
+ await db.query("INSERT INTO secrets (user_id, secret) VALUE (?,?)", [
+ req.session.uid,
+ req.body.secret.replace(/</g, "&lt;").replace(/>/g, "&gt;"),
+ ]);
+ res.redirect("/secrets");
+ } catch (e) {
+ console.error(e);
+ res.json("error");
+ }
+});
+
+app.get("/api/list", checkUser, async (req, res) => {
+ const secrets = await db.query(
+ "SELECT s.id, s.secret, c.name class, (s.user_id = ? OR ?) AS owner FROM secrets s INNER JOIN users u ON u.id = s.user_id INNER JOIN class c ON c.id = u.class_id",
+ [req.session.uid, req.session.isSuperAdmin || false],
+ );
+ res.json(secrets);
+});
+
+app.delete("/api/delete/:id", checkUser, async (req, res) => {
+ if (!req.params.id) return res.send("error");
+ try {
+ await db.query("DELETE FROM secrets WHERE id = ? AND (user_id = ? OR ?)", [
+ req.params.id,
+ req.session.uid,
+ req.session.isSuperAdmin || false,
+ ]);
+ res.send("ok");
+ } catch (e) {
+ console.error(e);
+ res.send("error");
+ }
+});
+
+module.exports = app;