123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import bodyParser from "body-parser";
- import express from "express";
- import session from "express-session";
- import cwh from "./Singletons.js";
- export default function UserInteractions(opts) {
- async function sha256(message) {
- // encode as UTF-8
- const msgBuffer = new TextEncoder().encode(message);
- // hash the message
- const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
- // convert ArrayBuffer to Array
- const hashArray = Array.from(new Uint8Array(hashBuffer));
- // convert bytes to hex string
- const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
- return hashHex;
- }
- let app = opts.app;
- app.use(session({
- secret: "rtifhg5878fj",
- resave: false,
- saveUninitialized: false,
- cookie: {
- sameSite: "lax", secure: "auto"
- }
- }))
- app.use(bodyParser.json({"limit": "200mb"}));
- app.use(express.json());
- let db = opts.db;
- app.options("/*", (req, res) => {
- res.set(cwh).end("FUCK YOU CORS")
- })
- app.get("/delacc", async (req, res) => {
- if (!req.session.uuid) {
- res.set(cwh).status(500).json({code: 500, R: "IO"})
- return
- }
- let uuid = req.session.uuid;
- db.execute("DELETE FROM user WHERE uuid = ?", [uuid], async (err, rows) => {
- if (err) {
- res.set(cwh).status(500).json({code: 500, R: "ERR"})
- req.session.destroy()
- } else {
- req.session.destroy()
- res.set(cwh).status(200).json({code: 200, R: "SUCCESS"})
- }
- })
- })
- app.get("/userapi", (req, res) => {
- if (!req.session.uuid) {
- 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(cwh).end(JSON.stringify({
- uuid: req.session.uuid,
- username: result[0].username,
- email: result[0].email,
- }));
- })
- })
- 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(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) {
- if (result.length === 0) {
- res.set(cwh).status(500).json({code: 500, R: "DNE"})
- return;
- }
- req.session.uuid = result[0].uuid;
- res.set(cwh).status(200).json({
- code: 200,
- R: "SS",
- uid: result[0].uuid
- });
- })
- }
- })
- 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 || !req.body.password) {
- res.set(cwh).status(500).json({code: 500, R: "PE"})
- return;
- }
- if (err) {
- console.log(err);
- res.set(cwh).status(500).json({code: 500, R: "UNE"});
- return;
- }
- if (rows.length === 0) {
- 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;
- }
- res.set(cwh).status(500).json({code: 500, R: "UE"});
- return;
- })
- })
- }
|