-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
299 lines (277 loc) · 8.45 KB
/
index.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const express = require("express");
const app = express();
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("./db/database.db");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const PORT = 8080;
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(
session({
secret: "wesdfksdf@#$ff",
resave: false,
saveUninitialized: false,
})
);
app.use((req, res, next) => {
let logged = "";
if (req.cookies.username) {
logged = req.cookies.username;
} else {
logged = "not logged";
}
db.run(`
insert into logs (path, user)
values ("${req.hostname}:${PORT}${req.url}", "${logged}")
`);
next();
});
app.set("view engine", "ejs");
app.get("/", (req, res) => {
res.render("home");
});
app.get("/login", (req, res) => {
if (req.cookies.username && req.cookies.username) {
res.redirect(`/user/${req.cookies.username}`);
} else if (req.cookies.username == "") {
res.render("login");
} else {
res.render("login");
}
});
app.post("/login", (req, res) => {
const { username, password } = req.body;
if (username == "" && password == "") {
res.send("username or password is empty");
} else {
db.all(
`select * from users where username = '${username}'`,
(err, rows) => {
if (rows.length == 0) {
res.send("username or password incorrect");
} else {
if (username === rows[0].username && password === rows[0].password) {
res.cookie("username", `${username}`);
res.redirect(`/user/${username}`);
} else {
res.send("username or password incorrect");
}
}
}
);
}
});
app.get("/signup", (req, res) => {
res.render("signup");
});
app.post("/signup", (req, res) => {
const { email, password, username } = req.body;
if (email == "" && username == "" && password == "") {
res.send("all inputs are required");
} else {
if (password[0] === password[1]) {
db.all(
`select * from users where username = '${username}'`,
(err, usernames) => {
if (usernames.length == 0) {
db.all(
`select * from users where email = '${email}'`,
(err, emails) => {
if (emails.length == 0) {
db.run(
`insert into users (email, password, username) values ('${email}', '${password[0]}', '${username}')`
);
res.redirect("/login");
} else {
res.send("this email already exist");
}
}
);
} else {
res.send("this username already exist");
}
}
);
} else {
res.send("password 1 not equal to password 2");
}
}
});
app.get("/user/:user", (req, res) => {
const { username } = req.cookies;
if (req.params.user == username) {
db.all(
`select * from users where username = '${username}'`,
(err, rows) => {
db.all(
`select * from projects where user = '${username}'`,
(err, projects) => {
if (rows[0].admin == "true") {
res.redirect(`/admins/${username}`);
} else {
res.render("user", { rows, projects });
}
}
);
}
);
} else {
db.all(
`select id, username, email from users where username = '${req.params.user}'`,
(err, user) => {
db.all(
`select * from projects where user = '${req.params.user}'`,
(err, projects) => {
res.render("guest", { user, projects });
}
);
}
);
}
});
app.get("/admins/:adminName", (req, res) => {
db.all("select * from users limit 10", (err, users) => {
db.all("select * from logs limit 10", (err, logs) => {
db.all("select * from projects limit 10", (err, projects) => {
db.all(
`select * from users where username = '${req.params.adminName}'`,
(err, admins) => {
const { username } = req.cookies;
if (admins.length == 0) {
res.redirect("/login");
} else if (admins[0].admin == "false") {
res.redirect("/login");
} else if (req.params.adminName != req.cookies.username) {
res.redirect("/login");
} else {
res.json({ users, logs, projects });
console.log(!req.cookies);
}
}
);
});
});
});
});
app.get("/editProfile", (req, res) => {
const { username } = req.cookies;
if (!req.cookies.username) {
res.redirect("/login");
} else {
db.all(
`select * from users where username = '${username}'`,
(err, rows) => {
res.render("editProfile", { rows });
}
);
}
});
app.post("/editProfile", (req, res) => {
const { username, email, password } = req.body;
db.all(
`select * from users where username = '${username}'`,
(err, usernames) => {
if (usernames.length == 0) {
db.all(
`select * from users where email = '${email}'`,
(err, emails) => {
if (emails.length == 0) {
if (password.length < 4) {
res.send("password length is minimal 4");
} else {
db.run(`
update users set username = "${username}",
email = "${email}",
password = "${password}" where username = "${req.cookies.username}"
`);
res.cookie("username", username);
res.redirect(`/user/${req.cookies.username}`);
}
} else {
res.send("this email already exist");
}
}
);
} else {
res.send("this username already exist");
}
}
);
});
app.get("/logout", (req, res) => {
res.clearCookie("username");
res.redirect("/login");
});
app.get("/addPoject", (req, res) => {
res.render("newPrject");
});
app.post("/addPoject/", (req, res) => {
const { name, description } = req.body;
if (name == "" && description == "") {
res.send("all inputs are required");
} else {
db.run(`
insert into projects (name, description, user)
values ('${name}', '${description}', '${req.cookies.username}')
`);
res.redirect("/login");
}
});
app.get("/getAllProjects", (req, res) => {
db.all("select * from projects", (err, rows) => {
res.json(rows);
});
});
app.get("/deleteProject/:delId", (req, res) => {
db.run(`DELETE FROM projects where id = ${req.params.delId}`);
res.redirect("/login");
});
app.get("/projectSettings/:id", (req, res) => {
res.send(req.params.id);
});
app.get("/serarchUser", (req, res) => {
db.all(
`select id, username, email from users where username = '${req.query.uName}'`,
(err, user) => {
db.all(
`select * from projects where user = '${req.query.uName}'`,
(err, projects) => {
// res.json({user, projects});
res.render("guest", { user, projects });
}
);
}
);
});
app.get("/comments/:id", (req, res) => {
db.all(
`select * from comments where projectId = "${req.params.id}" ORDER BY ID DESC`,
(err, comments) => {
let id = req.params.id;
res.render("comments", { comments, id });
// res.json(comments)
}
);
});
app.post("/addComment/:id", (req, res) => {
// res.send(req.body.comment);
const { username } = req.cookies;
if (!username) {
res.send("<h2>you are not logged <br> pleace <a href='/login'> Login </a></h2>");
} else {
db.run(
`insert into comments (user, comment, projectId) values ('${username}', '${req.body.comment}', '${req.params.id}')`
);
res.redirect(`/comments/${req.params.id}`);
}
});
app.get("/deleteAccount/:id/:username", (req, res) => {
db.run(`delete from users where id = ${req.params.id}`)
db.run(`delete from comments where user = '${req.params.username}'`)
db.run(`delete from projects where user = '${req.params.username}'`)
res.redirect("/logout")
})
app.listen(PORT, console.log(`http://localhost:${PORT}`));