Reaperxxxx commited on
Commit
189c642
·
verified ·
1 Parent(s): f3a0bb4

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +51 -19
server.js CHANGED
@@ -2,6 +2,7 @@ const express = require("express");
2
  const fs = require("fs");
3
  const path = require("path");
4
  const { exec } = require("child_process");
 
5
 
6
  const app = express();
7
  app.use(express.json());
@@ -21,36 +22,67 @@ function appendLog(username, log) {
21
  }
22
 
23
  // Endpoint to handle user sign-up
24
- app.post("/signup", (req, res) => {
25
  const { username, password } = req.body;
26
 
27
- const users = JSON.parse(fs.readFileSync(usersFile));
28
- if (users.some((user) => user.username === username)) {
29
- return res.status(400).send("User already exists.");
30
  }
31
 
32
- users.push({ username, password });
33
- fs.writeFileSync(usersFile, JSON.stringify(users));
34
-
35
- const userDir = path.join(usersDir, username);
36
- fs.mkdirSync(userDir);
37
- fs.writeFileSync(path.join(userDir, "logs.txt"), "");
38
-
39
- res.send("User signed up successfully.");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  });
41
 
42
  // Endpoint to handle login
43
- app.post("/login", (req, res) => {
44
  const { username, password } = req.body;
45
 
46
- const users = JSON.parse(fs.readFileSync(usersFile));
47
- const user = users.find((u) => u.username === username && u.password === password);
48
-
49
- if (!user) {
50
- return res.status(400).send("Invalid credentials.");
51
  }
52
 
53
- res.send("Login successful.");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  });
55
 
56
  // Endpoint to handle terminal commands
 
2
  const fs = require("fs");
3
  const path = require("path");
4
  const { exec } = require("child_process");
5
+ const bcrypt = require("bcrypt");
6
 
7
  const app = express();
8
  app.use(express.json());
 
22
  }
23
 
24
  // Endpoint to handle user sign-up
25
+ app.post("/signup", async (req, res) => {
26
  const { username, password } = req.body;
27
 
28
+ if (!username || !password) {
29
+ return res.status(400).send("Username and password are required.");
 
30
  }
31
 
32
+ try {
33
+ const usersData = JSON.parse(fs.readFileSync(usersFile));
34
+ if (usersData.some((user) => user.username === username)) {
35
+ return res.status(400).send("User already exists.");
36
+ }
37
+
38
+ // Hash the password
39
+ const hashedPassword = await bcrypt.hash(password, 10);
40
+
41
+ // Add new user
42
+ usersData.push({ username, password: hashedPassword });
43
+ fs.writeFileSync(usersFile, JSON.stringify(usersData, null, 2));
44
+
45
+ // Create user directory and logs file
46
+ const userDir = path.join(usersDir, username);
47
+ if (!fs.existsSync(userDir)) {
48
+ fs.mkdirSync(userDir, { recursive: true });
49
+ }
50
+ fs.writeFileSync(path.join(userDir, "logs.txt"), "");
51
+
52
+ res.send("User signed up successfully.");
53
+ } catch (error) {
54
+ console.error("Error during signup:", error);
55
+ res.status(500).send("Internal server error.");
56
+ }
57
  });
58
 
59
  // Endpoint to handle login
60
+ app.post("/login", async (req, res) => {
61
  const { username, password } = req.body;
62
 
63
+ if (!username || !password) {
64
+ return res.status(400).send("Username and password are required.");
 
 
 
65
  }
66
 
67
+ try {
68
+ const usersData = JSON.parse(fs.readFileSync(usersFile));
69
+ const user = usersData.find((u) => u.username === username);
70
+
71
+ if (!user) {
72
+ return res.status(400).send("Invalid username or password.");
73
+ }
74
+
75
+ // Compare the provided password with the hashed password
76
+ const isPasswordValid = await bcrypt.compare(password, user.password);
77
+ if (!isPasswordValid) {
78
+ return res.status(400).send("Invalid username or password.");
79
+ }
80
+
81
+ res.send("Login successful.");
82
+ } catch (error) {
83
+ console.error("Error during login:", error);
84
+ res.status(500).send("Internal server error.");
85
+ }
86
  });
87
 
88
  // Endpoint to handle terminal commands