AXCWG 3 månader sedan
förälder
incheckning
a921190dd3
1 ändrade filer med 13 tillägg och 10 borttagningar
  1. 13 10
      UserInteractions.js

+ 13 - 10
UserInteractions.js

@@ -1,7 +1,6 @@
 const bodyParser = require("body-parser");
 const express = require("express");
 let session = require('express-session')
-let singleton = require('./Singletons')
 const {cwh} = require("./Singletons");
 module.exports = function UserInteractions(opts) {
     async function sha256(message) {
@@ -32,16 +31,16 @@ module.exports = function UserInteractions(opts) {
     app.use(express.json());
 
     let db = opts.db;
-    app.options("/login", (req, res) => {
-        res.set(singleton.cwh).end("FUCK YOU CORS")
+    app.options("/*", (req, res) => {
+        res.set(cwh).end("FUCK YOU CORS")
     })
     app.get("/userapi", (req, res) => {
         if (!req.session.uuid) {
-            res.set(singleton.cwh).status(500).json({code: 500, R: "IO"})
+            res.set(cwh).status(500).json({code: 500, R: "IO"})
             return
         }
         db.execute("SELECT username, email from user where uuid = ?", [req.session.uuid], (err, result) => {
-            res.set(singleton.cwh).end(JSON.stringify({
+            res.set(cwh).end(JSON.stringify({
                 uuid: req.session.uuid,
                 username: result[0].username,
                 email: result[0].email,
@@ -49,9 +48,13 @@ module.exports = function UserInteractions(opts) {
 
         })
     })
+    app.get("/logout", (req, res) => {
+        req.session.destroy()
+        res.set(cwh).status(200)
+    })
     app.post("/login", async function (req, res) {
         if (!req.body.username || !req.body.password) {
-            res.set(singleton.cwh).status(500).json({code: 500, R: "IO"})
+            res.set(cwh).status(500).json({code: 500, R: "IO"})
 
         } else {
             db.execute("SELECT uuid from user where username = ? and password = ?", [req.body.username, await sha256(req.body.password)], function (err, result) {
@@ -61,7 +64,7 @@ module.exports = function UserInteractions(opts) {
                 }
                 req.session.uuid = result[0].uuid;
 
-                res.set(singleton.cwh).status(200).json({
+                res.set(cwh).status(200).json({
                     code: 200,
                     R: "SS",
                     uid: result[0].uuid
@@ -73,7 +76,7 @@ module.exports = function UserInteractions(opts) {
     })
     app.post("/register", function (req, res) {
         db.execute("SELECT uuid FROM user WHERE username = ?", [req.body.username], async function (err, rows) {
-            if (req.body.username === undefined || req.body.username === "" || req.body.username === null || req.body.password === undefined || req.body.password === "" || req.body.password === null) {
+            if (!req.body.username || !req.body.password) {
                 res.set(cwh).status(500).json({code: 500, R: "PE"})
                 return;
             }
@@ -83,8 +86,8 @@ module.exports = function UserInteractions(opts) {
                 return;
             }
             if (rows.length === 0) {
-                db.execute("INSERT INTO user (uuid, username, email, password, avatar, time) values (?,?,?,?,?,?)", [crypto.randomUUID(), req.body.username, null, await sha256(req.body.password), null, Date.now()]);
-                res.status(200).set(singleton.cwh).json({code: 200, R: "SS"});
+                db.execute("INSERT INTO user (uuid, username, email, password, avatar, time) values (?,?,?,?,?,?)", [crypto.randomUUID(), req.body.username, !req.body.email ? null : req.body.email, await sha256(req.body.password), null, Date.now()]);
+                res.status(200).set(cwh).json({code: 200, R: "SS"});
 
                 return;
             }