-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
198 lines (158 loc) · 5.25 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// server.js
const express = require("express");
const sqlite3 = require("sqlite3");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const PORT = process.env.PORT || 3001;
// Set EJS as the view engine
app.set("view engine", "ejs");
app.set("views", `${__dirname}/views`);
app.use(bodyParser.json());
app.use(
cors({
origin: "*", // Add your frontend URL
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true,
})
);
// Use a persistent SQLite database instead of in-memory
const db = new sqlite3.Database("crypto-miner.db", (err) => {
if (err) {
console.error("Error opening database:", err.message);
} else {
console.log("Connected to the SQLite database.");
// Create the users table if it does not exist
db.run(
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT, password TEXT, approved BOOLEAN DEFAULT 0, allow_withdraw BOOLEAN NOT NULL DEFAULT 0 )"
);
}
});
app.get("/", (req, res) => {
// Fetch the list of users from the database
db.all("SELECT * FROM users", (err, users) => {
if (err) {
return res.status(500).json({ error: "Internal server error" });
}
// Render the admin dashboard page
res.render("adminDashboard", { users });
});
});
app.post("/admin/revoke/withdrawal/:userId", (req, res) => {
const userId = req.params.userId;
console.log(userId);
// Update the user's 'withdrawal' status in the database
db.run(
"UPDATE users SET allow_withdraw = 0 WHERE id = ?",
[userId],
(err) => {
if (err) {
console.log("[DB - ERROR]: ", err);
return res.status(500).json({ error: "Internal server error" });
}
// Redirect back to the admin dashboard
res.redirect("/");
}
);
});
app.post("/admin/approve/withdrawal/:userId", (req, res) => {
const userId = req.params.userId;
console.log(userId);
// Update the user's 'withdrawal' status in the database
db.run(
"UPDATE users SET allow_withdraw = 1 WHERE id = ?",
[userId],
(err) => {
if (err) {
console.log("[DB - ERROR]: ", err);
return res.status(500).json({ error: "Internal server error" });
}
// Redirect back to the admin dashboard
res.redirect("/");
}
);
});
app.post("/admin/approve/:userId", (req, res) => {
const userId = req.params.userId;
// Update the user's 'approved' status in the database
db.run(
"UPDATE users SET approved = 1 WHERE id = ?",
[userId],
(err) => {
if (err) {
console.log("[DB - ERROR]: ", err);
return res.status(500).json({ error: "Internal server error" });
}
// Redirect back to the admin dashboard
res.redirect("/");
}
);
});
app.post("/api/withdrawal/approval/:userEmail", (req, res) => {
const userEmail = req.params.userEmail;
// Check if user has VIP/Priviledged withdrawal enabled
db.get("SELECT * FROM users WHERE email = ?", [userEmail], (err, row) => {
if (err) {
console.log("[DB - ERROR]: ", err);
return res.status(500).json({ error: "Internal server error" });
}
const status = row.allow_withdraw ? true : false;
res.status(200).json({
withdrawal_status: status,
msg: status ? 'Withdrawal Approved' : "User Not VIP Approved",
});
})
});
app.post("/api/auth", (req, res) => {
const { email, password } = req.body;
// Check if the user already exists
db.get("SELECT * FROM users WHERE email = ?", [email], (err, row) => {
if (err) {
return res.status(500).json({ error: "Internal server error" });
}
if (row) {
// User exists, check if approved
if (row.approved) {
// User is approved, perform login
db.get(
"SELECT id, email FROM users WHERE email = ? AND password = ?",
[email, password],
(loginErr, loginRow) => {
if (loginErr) {
return res.status(500).json({ error: "Internal server error" });
}
if (!loginRow) {
return res.status(401).json({ error: "Invalid credentials" });
}
return res.status(200).json({
message: "Login successful",
user: { id: loginRow.id, email: loginRow.email },
});
}
);
} else {
// User is not approved
return res.status(401).json({ error: "User not approved" });
}
} else {
// User doesn't exist, perform registration
db.run(
"INSERT INTO users (email, password) VALUES (?, ?)",
[email, password],
function (registerErr) {
if (registerErr) {
return res.status(500).json({ error: "Failed to register user" });
}
const userId = this.lastID; // Get the last inserted row ID
return res.status(200).json({
message: "User registered successfully click again to login",
user: { id: userId, email },
});
}
);
}
});
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});