UserInteractions.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import bodyParser from "body-parser";
  2. import express from "express";
  3. import session from "express-session";
  4. import cwh from "./Singletons.js";
  5. export default function UserInteractions(opts) {
  6. async function sha256(message) {
  7. // encode as UTF-8
  8. const msgBuffer = new TextEncoder().encode(message);
  9. // hash the message
  10. const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
  11. // convert ArrayBuffer to Array
  12. const hashArray = Array.from(new Uint8Array(hashBuffer));
  13. // convert bytes to hex string
  14. const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
  15. return hashHex;
  16. }
  17. let app = opts.app;
  18. app.use(session({
  19. secret: "rtifhg5878fj",
  20. resave: false,
  21. saveUninitialized: false,
  22. cookie: {
  23. sameSite: "lax", secure: "auto"
  24. }
  25. }))
  26. app.use(bodyParser.json({"limit": "200mb"}));
  27. app.use(express.json());
  28. let db = opts.db;
  29. app.options("/*", (req, res) => {
  30. res.set(cwh).end("FUCK YOU CORS")
  31. })
  32. app.get("/delacc", async (req, res) => {
  33. if (!req.session.uuid) {
  34. res.set(cwh).status(500).json({code: 500, R: "IO"})
  35. return
  36. }
  37. let uuid = req.session.uuid;
  38. db.execute("DELETE FROM user WHERE uuid = ?", [uuid], async (err, rows) => {
  39. if (err) {
  40. res.set(cwh).status(500).json({code: 500, R: "ERR"})
  41. req.session.destroy()
  42. } else {
  43. req.session.destroy()
  44. res.set(cwh).status(200).json({code: 200, R: "SUCCESS"})
  45. }
  46. })
  47. })
  48. app.get("/userapi", (req, res) => {
  49. if (!req.session.uuid) {
  50. res.set(cwh).status(500).json({code: 500, R: "IO"})
  51. return
  52. }
  53. db.execute("SELECT username, email from user where uuid = ?", [req.session.uuid], (err, result) => {
  54. res.set(cwh).end(JSON.stringify({
  55. uuid: req.session.uuid,
  56. username: result[0].username,
  57. email: result[0].email,
  58. }));
  59. })
  60. })
  61. app.get("/logout", (req, res) => {
  62. req.session.destroy()
  63. res.set(cwh).status(200)
  64. })
  65. app.post("/login", async function (req, res) {
  66. if (!req.body.username || !req.body.password) {
  67. res.set(cwh).status(500).json({code: 500, R: "IO"})
  68. } else {
  69. db.execute("SELECT uuid from user where username = ? and password = ?", [req.body.username, await sha256(req.body.password)], function (err, result) {
  70. if (result.length === 0) {
  71. res.set(cwh).status(500).json({code: 500, R: "DNE"})
  72. return;
  73. }
  74. req.session.uuid = result[0].uuid;
  75. res.set(cwh).status(200).json({
  76. code: 200,
  77. R: "SS",
  78. uid: result[0].uuid
  79. });
  80. })
  81. }
  82. })
  83. app.post("/playlist", async function (req, res) {
  84. if (!req.session.uuid) {
  85. res.set(cwh).status(500).json({code: 500, R: "AD"})
  86. return;
  87. }
  88. db.execute("SELECT title, tmb, content from playlist where uuid = ?", [req.session.uuid], function (err, result) {
  89. if (err) {
  90. console.log(err);
  91. res.set(cwh).status(500).json({code: 500, R: "AD"})
  92. return;
  93. }
  94. res.set(cwh).end(JSON.stringify(result));
  95. })
  96. })
  97. app.post("/register", function (req, res) {
  98. db.execute("SELECT uuid FROM user WHERE username = ?", [req.body.username], async function (err, rows) {
  99. if (!req.body.username || !req.body.password) {
  100. res.set(cwh).status(500).json({code: 500, R: "PE"})
  101. return;
  102. }
  103. if (err) {
  104. console.log(err);
  105. res.set(cwh).status(500).json({code: 500, R: "UNE"});
  106. return;
  107. }
  108. if (rows.length === 0) {
  109. 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()]);
  110. res.status(200).set(cwh).json({code: 200, R: "SS"});
  111. return;
  112. }
  113. res.set(cwh).status(500).json({code: 500, R: "UE"});
  114. return;
  115. })
  116. })
  117. }