-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
371 lines (303 loc) · 8.14 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = 8080;
const mysql = require("mysql");
const listEndpoints = require("express-list-endpoints")
const fs = require("fs");
const jwt = require("jsonwebtoken");
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(express.json())
const httpServer = app.listen(port, function () {
console.log(`Web server is running on port ${port}`);
});
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "forum",
multipleStatements: true,
});
db.connect((err) => {
if (err) {
throw err;
}
console.log("Connected to the database!");
});
const crypto = require("crypto");
function hash(data) {
const hash = crypto.createHash("sha256");
hash.update(data);
return hash.digest("hex");
}
app.get("/endpoints", (req, res) => {
res.send(listEndpoints(app))
});
app.get("/login-new-user", function (req, res) {
res.sendFile(__dirname + "/login.html");
});
app.get("/login", (req, res) => {
res.sendFile(__dirname + "/userlogin.html");
});
// LOGIN NEW USER --> hash password
app.post("/login-new-user", function (req, res) {
if (!(req.body && req.body.username && req.body.password && req.body.name)) {
res.status(400).send({
success: false,
error: "Name, username & password is required!"
});
return;
}
let fields = ["name", "password", "username"];
for (let key in req.body) {
if (!fields.includes(key)) {
res.status(400).send({
success: false,
error: "Unknown field: " + key
});
return;
}
}
let sql = `
INSERT INTO users (username, name, password)
VALUES ('${req.body.username}', '${req.body.name}', '${hash(req.body.password)}');
SELECT LAST_INSERT_ID();`;
console.log(sql);
db.query(sql, function (err, result, fields) {
if (err) {
console.error(err);
res.status(500).json({
success: false,
error: "Server Error"
});
return;
}
res.redirect("/forum");
// res.status(200).json({ success: true, data: output });
});
});
// LOGIN EXISTING USER ---> hash password
app.post("/login", function (req, res) {
if (!(req.body && req.body.username && req.body.password && req.body.name)) {
res.status(400).json({
success: false,
message: "Bad Request: Missing credentials"
});
return;
}
let sql = `
SELECT * FROM users WHERE username='${req.body.username}'`;
db.query(sql, function (err, result, fields) {
if (err) {
console.error(err);
res.status(500).json({
success: false, error: "Server Error"
});
return;
}
if (result.length > 0) {
let passwordHash = hash(req.body.password);
if (result[0].password == passwordHash) {
let payload = {
sub: result[0].username,
name: result[0].name,
};
let token = jwt.sign(payload, "EnHemlighetSomIngenKanGissaXyz123%&/");
// token in a cookie
res.cookie('token', token);
res.redirect("/forum");
} else {
res.status(401).json({
success: false,
message: "Unauthorized: wrong credentials"
});
}
} else {
res.status(401).json({
success: false,
message: "Unauthorized: User not found"
});
}
});
});
// GET USERS
app.get("/users", function (req, res) {
let authHeader = req.headers["authorization"];
if (authHeader === undefined) {
res.sendStatus(400);
return;
}
let token = authHeader.slice(7);
let decoded;
try {
decoded = jwt.verify(token, "EnHemlighetSomIngenKanGissaXyz123%&/");
} catch (err) {
console.log(err);
res.status(401).send("Invalid auth token");
return;
}
let sql = "SELECT * FROM users";
console.log(sql);
db.query(sql, function (err, result, fields) {
res.send(result);
});
});
// GET USER BY ID
app.get("/users/:id", function (req, res) {
let authHeader = req.headers["authorization"];
if (authHeader === undefined) {
res.sendStatus(400);
return;
}
let token = authHeader.slice(7);
let decoded;
try {
decoded = jwt.verify(token, "EnHemlighetSomIngenKanGissaXyz123%&/");
} catch (err) {
console.log(err);
res.status(401).send("Invalid auth token");
return;
}
// Get the user ID from the request parameters
const userId = req.params.id;
let sql = `SELECT * FROM users WHERE user_id = ?`;
db.query(sql, [userId], function (err, result, fields) {
if (err) {
console.log(err);
res.sendStatus(500);
return;
}
if (result.length === 0) {
res.sendStatus(404);
return;
}
res.send(result[0]);
});
});
//Display all posts at forum
app.get("/forum", (req, res) => {
const sql = "SELECT * FROM posts";
db.query(sql, (err, posts) => {
if (err) {
console.error(err);
res.status(500).send("Internal Server Error");
return;
}
res.sendFile(__dirname + "/forum.html", { posts });
});
});
// Get all posts as json to fetch
app.get("/getposts", (req, res) => {
const sql = "SELECT * FROM posts";
db.query(sql, (err, posts) => {
if (err) {
console.error(err);
res.status(500).send("Server Error get all posts");
return;
}
res.json(posts);
});
});
// Add new post
app.post("/addpost", (req, res) => {
const { heading, body, author } = req.body;
let sql = `INSERT INTO posts (heading, body, author)
VALUES ('${heading}', '${body}', '${author}')`;
db.query(sql, (err, result) => {
if (err) {
console.error(err);
res.status(500).send("Internal Server Error");
return;
}
res.redirect("/forum");
});
});
//get post as json, fetch in client
app.get("/getpost/:postId", (req, res) => {
const postId = req.params.postId;
let postSql =
"SELECT * FROM posts WHERE post_id = ?";
let commentsSql =
"SELECT * FROM comments WHERE post_id = ?";
db.query(postSql, [postId], (err, posts) => {
if (err) {
console.error(err);
res.status(500).send("Server Error, getpost");
return;
}
db.query(commentsSql, [postId], (err, comments) => {
if (err) {
console.error(err);
res.status(500).send("Server Error, getpost");
return;
}
const post = posts[0];
post.comments = comments;
res.json(post);
});
});
});
// GET POST BY ID & ITS COMMENTS
app.get("/viewpost/:postId", (req, res) => {
const { postId } = req.params
let postSql =
`SELECT * FROM posts WHERE post_id = ${postId}`;
let commentsSql =
`SELECT * FROM comments WHERE post_id = ${postId}`;
db.query(postSql, [postId], (err, posts) => {
if (err) {
console.error(err);
res.status(500).json({
success: false,
error: "Server Error"
});
return;
}
if (!posts || posts.length === 0) {
res.status(400).json({
success: false,
error: ` No post with id ${postId} found`
});
return;
}
db.query(commentsSql, [postId], (err, comments) => {
if (err) {
console.error(err);
res.status(500).json({
success: false,
error: "Server Error"
});
return;
}
const post = posts[0];
post.comments = comments;
// res.status(200).json({
// success: true,
// data: post
// // (comments --> to display only comments belonging to chosen post)
// });
res.sendFile(__dirname + "/viewpost.html", post);
});
});
});
// ADD COMMENT
app.post("/addcomment", (req, res) => {
try {
const { post_id, comment, author } = req.body;
const sql = `
INSERT INTO comments (post_id, comment, author)
VALUES ('${req.body.post_id}', '${req.body.comment}', '${req.body.author}')`
db.query(sql, [post_id, comment, author], (err, result, fields) => {
if (err) {
res.status(500).json({ sucess: "Server Error 1" });
return;
}
res.json({ success: true });
});
} catch (error) {
console.error(error);
res.status(500).json({ sucess: "Server Error 2" });
}
});