Update server.js
Browse files
server.js
CHANGED
|
@@ -23,65 +23,41 @@ function appendLog(username, log) {
|
|
| 23 |
|
| 24 |
// Endpoint to handle user sign-up
|
| 25 |
app.post("/signup", async (req, res) => {
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 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 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
});
|
| 87 |
|
|
@@ -113,15 +89,26 @@ app.post("/execute", (req, res) => {
|
|
| 113 |
|
| 114 |
// Endpoint to get user logs
|
| 115 |
app.get("/logs", (req, res) => {
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
});
|
| 126 |
|
| 127 |
// Endpoint for "Deploy Kaizen"
|
|
|
|
| 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 |
+
// Ensure the users file exists
|
| 34 |
+
if (!fs.existsSync(usersFile)) {
|
| 35 |
+
fs.writeFileSync(usersFile, JSON.stringify([]));
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
const usersData = JSON.parse(fs.readFileSync(usersFile));
|
| 39 |
+
if (usersData.some((user) => user.username === username)) {
|
| 40 |
+
return res.status(400).send("User already exists.");
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// Hash the password
|
| 44 |
+
const hashedPassword = await bcrypt.hash(password, 10);
|
| 45 |
+
|
| 46 |
+
// Add new user
|
| 47 |
+
usersData.push({ username, password: hashedPassword });
|
| 48 |
+
fs.writeFileSync(usersFile, JSON.stringify(usersData, null, 2));
|
| 49 |
+
|
| 50 |
+
// Create user directory and logs file
|
| 51 |
+
const userDir = path.join(usersDir, username);
|
| 52 |
+
if (!fs.existsSync(userDir)) {
|
| 53 |
+
fs.mkdirSync(userDir, { recursive: true });
|
| 54 |
+
}
|
| 55 |
+
fs.writeFileSync(path.join(userDir, "logs.txt"), "");
|
| 56 |
+
|
| 57 |
+
res.status(200).json({ message: "User signed up successfully." });
|
| 58 |
+
} catch (error) {
|
| 59 |
+
console.error("Error during signup:", error);
|
| 60 |
+
res.status(500).send("Internal server error.");
|
| 61 |
}
|
| 62 |
});
|
| 63 |
|
|
|
|
| 89 |
|
| 90 |
// Endpoint to get user logs
|
| 91 |
app.get("/logs", (req, res) => {
|
| 92 |
+
const { username } = req.query;
|
| 93 |
|
| 94 |
+
if (!username) {
|
| 95 |
+
return res.status(400).send("Username is required.");
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
const userDir = path.join(usersDir, username);
|
| 99 |
+
const logsFile = path.join(userDir, "logs.txt");
|
| 100 |
|
| 101 |
+
if (!fs.existsSync(logsFile)) {
|
| 102 |
+
return res.status(404).send("Logs not found.");
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
try {
|
| 106 |
+
const logs = fs.readFileSync(logsFile, "utf8");
|
| 107 |
+
res.send(logs);
|
| 108 |
+
} catch (error) {
|
| 109 |
+
console.error("Error fetching logs:", error);
|
| 110 |
+
res.status(500).send("Failed to fetch logs.");
|
| 111 |
+
}
|
| 112 |
});
|
| 113 |
|
| 114 |
// Endpoint for "Deploy Kaizen"
|