-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
692 lines (649 loc) · 21.1 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
const { Pool } = pg;
// import { render } from "ejs";
import env from "dotenv";
import bcrypt from "bcrypt";
import passport from "passport";
import { Strategy } from "passport-local";
import GoogleStrategy from "passport-google-oauth2";
import FacebookStrategy from "passport-facebook";
import session from "express-session";
env.config(); // Load environment variables
const app = express();
const port = process.env.PORT;
const saltRounds = 10;
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
})
);
// Set up the PostgreSQL connection pool
// console.log("DB_URL", process.env.DB_URL);
// const db = new Pool({
// connectionString: process.env.DB_URL,
// ssl: {
// rejectUnauthorized: false
// }
// });
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(passport.initialize());
app.use(passport.session());
const db = new pg.Client({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
});
db.connect();
let posts = [
{
id: 1,
user_id: 1,
isbn: "0-13-449837-2" ,
rating: 10,
title: "Starting Out with C++ from Control Structures to Objects",
review: "This is my first book to learn C++",
date: "2024-01-01T14:30:00Z",
},
];
//to hold the current user's information
let state = "home";
let username = "Peter";
let user_id = 1;
//temporary store user's registration info.
let email = "";
let password = "";
//to hold the state of the login
let isLogin = false;
//bring user to home page
//trigerred when the app load initially / user use the search / sort button
app.get("/", async (req, res) => {
console.log("GOOGLE_CLIENT_ID: ", process.env.GOOGLE_CLIENT_ID);
console.log("GOOGLE_CLIENT_SECRET: ", process.env.GOOGLE_CLIENT_SECRET);
console.log("FACEBOOK_CLIENT_ID: ", process.env.FACEBOOK_CLIENT_ID);
console.log("FACEBOOK_CLIENT_SECRET: ", process.env.FACEBOOK_CLIENT_SECRET);
console.log("req.body.menu: ", req.body.menu);
console.log("app.get("/", async (req, res)");
console.log("state: ", state);
try{
const result = await db.query(
"SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id ORDER BY books.id ASC;"
);
posts = [];
posts = result.rows;
console.log(posts);
res.render("index.ejs", {
posts:posts,
state: state,
isLogin: isLogin,
});
} catch(err){
console.log("Error to access database");
console.log(err.stack);
}
});
//triggered when home button or "My Note" button is pressed
//show the My Note page
app.get("/me", async (req, res) => {
console.log(req.user);
if (req.isAuthenticated()) {
state = "user";
console.log("state: ", state);
console.log("userID: ", user_id);
try{
const result = await db.query(
"SELECT * FROM books WHERE user_id = $1 ORDER BY id ASC",
[user_id]
);
posts = [];
posts = result.rows;
console.log(posts);
res.render("index.ejs", {
posts:posts,
state: state,
username: username,
isLogin: isLogin,
});
} catch(err){
console.log(err.stack);
}
} else {
res.redirect("/login");
}
});
//triggered when user click on the home button or search button
app.post("/action", (req, res) => {
if (req.body.menu == "Home"){
state = "home";
} else if (req.body.menu == "Search"){
state = "search";
}
res.redirect("/");
})
//user search the book via isbn number or the book title
//show the search result
app.post("/search", async (req, res) =>{
let input = req.body.search_result;
let type;
console.log("input: ", input);
console.log("state: ", state);
if (parseInt(input)){
//user wish to search the book via isbn number
type = "isbn";
} else{
//user wish to search the book via book title
type = "title";
}
console.log("type: ", type);
if (state == "home"){ //if user use search function in home page
if (type == "title"){
try{
const result = await db.query(
"SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id WHERE LOWER(books.title) LIKE '%' || $1 || '%';",
[input.toLowerCase()]
);
posts = [];
posts = result.rows;
console.log(posts);
if (posts.length == 0){
//search result = 0
res.render("index.ejs",{
posts:posts,
state: state,
message: "No results found for your search.",
search: true,
isLogin: isLogin,
});
} else{
res.render("index.ejs",{
posts:posts,
state: state,
search: true,
isLogin: isLogin,
});
}
} catch (err){
console.log(err.stack);
}
} else if (type == "isbn"){
try{
const result = await db.query(
"SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id WHERE books.isbn = $1;",
[input]
);
posts = [];
posts = result.rows;
console.log(posts);
if (posts.length == 0){
//search result = 0
res.render("index.ejs",{
posts:posts,
state: state,
message: "No results found for your search.",
search: true,
isLogin: isLogin,
});
} else{
res.render("index.ejs",{
posts:posts,
state: state,
search: true,
isLogin: isLogin,
});
}
} catch (err){
console.log(err.stack);
}
}
} else if (state == "user"){ //if user uses search function in their own user page
if (type == "title"){
//user wish to search the book via isbn number
try{
const result = await db.query(
"SELECT * FROM books WHERE LOWER(title) LIKE '%' || $1 || '%' AND user_id = $2;",
[input.toLowerCase(), user_id]
);
posts = [];
posts = result.rows;
console.log(posts);
if (posts.length == 0){
//search result = 0
res.render("index.ejs",{
posts:posts,
state: state,
message: "No results found for your search.",
search: true,
isLogin: isLogin,
});
} else{
res.render("index.ejs",{
posts:posts,
state: state,
search: true,
isLogin: isLogin,
});
}
} catch (err){
console.log(err.stack);
}
} else if (type == "isbn"){
//user wish to search the book via book title
try{
const result = await db.query(
"SELECT * FROM books WHERE isbn = $1 AND user_id = $2;",
[input, user_id]
);
posts = [];
posts = result.rows;
console.log(posts);
if (posts.length == 0){
//search result = 0
res.render("index.ejs",{
posts:posts,
state: state,
error: "No results found for your search.",
search: true,
isLogin: isLogin,
});
} else{
res.render("index.ejs",{
posts:posts,
state: state,
search: true,
isLogin: isLogin,
});
}
} catch (err){
console.log(err.stack);
}
}
}
});
//show user the sort result
app.post("/sort", async (req, res) => {
const sortType = req.body.sort;
console.log("state: ", state);
console.log("sort by: ", sortType);
let query;
let message;
if (state == "home"){
if (sortType == "Book title (A-Z)"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id ORDER BY title ASC;";
} else if (sortType == "Date: Oldest First"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id ORDER BY books.id ASC;;";
} else if (sortType == "Rating: Low to High"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id ORDER BY rating ASC;";
} else if (sortType == "Book title (Z-A)"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id ORDER BY title DESC;";
} else if (sortType == "Date: Newest First"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id ORDER BY books.id DESC;";
} else if (sortType == "Rating: High to Low"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id ORDER BY rating DESC;";
}
try{
console.log("query: ", query);
const result = await db.query(query);
posts = [];
posts = result.rows;
// Log the raw result
console.log("Raw query result: ", result.rows);
console.log("Processed posts: ", posts);
if (posts.length == 0){
message = "You have no book review yet. Start your first one by today ~~";
}
res.render("index.ejs",
{
posts:posts,
state: state,
message: message,
isLogin: isLogin,
}
);
} catch (err){
console.log(err.stack);
}
} else if (state == "user"){
if (sortType == "Book title (A-Z)"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id WHERE users.username = $1 ORDER BY title ASC;";
} else if (sortType == "Date: Oldest First"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id WHERE users.username = $1 ORDER BY books.id ASC;;";
} else if (sortType == "Rating: Low to High"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id WHERE users.username = $1 ORDER BY rating ASC;";
} else if (sortType == "Book title (Z-A)"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id WHERE users.username = $1 ORDER BY title DESC;";
} else if (sortType == "Date: Newest First"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id WHERE users.username = $1 ORDER BY books.id DESC;";
} else if (sortType == "Rating: High to Low"){
query = "SELECT books.id, books.user_id, books.isbn, books.rating, books.title, books.review, books.date, users.username FROM books JOIN users ON books.user_id = users.id WHERE users.username = $1 ORDER BY rating DESC;";
}
try{
console.log("query: ", query);
const result = await db.query(query, [username]);
posts = [];
posts = result.rows;
// Log the raw result
console.log("Raw query result: ", result.rows);
console.log("Processed posts: ", posts);
if (posts.length == 0){
message = "You have no book review yet. Start your first one by today ~~";
}
res.render("index.ejs",
{
posts:posts,
state: state,
message: message,
isLogin: isLogin,
}
);
} catch (err){
console.log(err.stack);
}
}
});
//show user the add review page
app.get("/new", (req, res) => {
console.log(req.user);
if (req.isAuthenticated()) {
res.render("partials/modify.ejs", { heading: "New Book", submit: "Add a new book" });
} else {
res.redirect("/login");
}
});
//show user the edit review page
//pass the details of the review to the modify.ejs
app.post("/edit", async (req, res) => {
const updateID = req.body.updatedPostId;
try {
const response = await db.query(
"SELECT * FROM books WHERE id = $1;",
[updateID]
);
posts = [];
posts = response.rows[0];
console.log(posts.data);
res.render("partials/modify.ejs", {
heading: "Edit Review",
submit: "Update Review",
post: posts,
});
} catch (error) {
console.log(err.stack);
}
});
//process and update the book review
app.post("/update", async (req, res) => {
const updateID = req.body.id;
const updateTitle = req.body.title;
const updateISBN = req.body.isbn;
const updateReview = req.body.review;
const updateRating = req.body.rating;
console.log("updateID: ", updateID);
console.log("updateTitle: ", updateTitle);
console.log("updateReview: ", updateReview);
console.log("updateRating: ", updateRating);
try{
await db.query(
"UPDATE books SET title = ($1), isbn = ($2), review = ($3), rating = ($4) WHERE id = ($5);",
[updateTitle, updateISBN, updateReview, updateRating, updateID]
);
res.redirect("/me");
} catch (err){
console.log(err.stack);
}
});
//process and add a new book review
app.post("/posts", async(req, res) => {
const title = req.body.title;
const isbn = req.body.isbn;
const review = req.body.review;
const rating = req.body.rating;
const date = new Date();
console.log("title: ", title);
console.log("isbn: ", isbn);
console.log("review: ", review);
console.log("rating: ", rating);
try{
const result = await db.query(
"SELECT * FROM books WHERE isbn = $1 AND user_id = $2",
[isbn, user_id]
);
posts = [];
posts = result.rows;
console.log(posts);
if (posts.length != 0){
console.log("Repeated book review detected.")
res.render("modify.ejs",{
error: "⚠ You have already reviewed this book. Please add a new one.",
heading: "New Book",
submit: "Add a new book"
}
);
return;
}
} catch (err){
console.log(err.stack);
}
try{
await db.query(
"INSERT INTO books (user_id, isbn, rating, title, review, date) VALUES ($1, $2, $3, $4, $5, $6);",
[user_id, isbn, rating, title, review, date]
);
res.redirect("/me");
} catch (err){
console.log(err.stack);
}
});
app.post("/delete", async (req, res) => {
const deleteID = req.body.deletePostId;
try{
await db.query(
"DELETE FROM books WHERE id = ($1);",
[deleteID]
);
res.redirect("/me");
} catch (err){
console.log(err.stack);
}
});
//bring user to sign up/login page
app.get("/login", (req, res) => {
res.render("partials/signup-and-login.ejs");
});
//bring user to home page after log out
app.get("/logout", (req, res) => {
isLogin = false;
req.logout(function (err) {
if (err) {
return next(err);
}
console.log("logout successfully!");
state = "home";
username = "";
user_id = 0;
res.redirect("/");
});
});
app.get("/auth/google", passport.authenticate("google", {
scope: ["profile", "email"]
}));
app.get("/auth/google/me", passport.authenticate("google", {
successRedirect: "/me",
failureRedirect: "/login",
}));
app.get("/auth/facebook", passport.authenticate("facebook", {
scope: ["public_profile", "email"]
}));
app.get("/auth/facebook/me", passport.authenticate("facebook", {
successRedirect: "/me",
failureRedirect: "/login",
}));
//determine whether the login details are correct after user submission (local strategy)
app.post(
"/login",
passport.authenticate("local", {
successRedirect: "/me",
failureRedirect: "/login",
})
);
//handle new user details after submission (local strategy)
app.post("/register", async (req, res) => {
email = req.body.email;
password = req.body.password;
username = req.body.username;
try {
const checkResult = await db.query("SELECT * FROM users WHERE email = $1", [
email,
]);
if (checkResult.rows.length > 0) {
res.redirect("/login");
} else {
bcrypt.hash(password, saltRounds, async (err, hash) => {
if (err) {
console.error("Error hashing password:", err);
} else {
const result = await db.query(
"INSERT INTO users (username, email, password) VALUES ($1, $2, $3) RETURNING *",
[username, email, hash]
);
const user = result.rows[0];
user_id = user.id;
req.login(user, (err) => {
console.log("success");
isLogin = true;
res.redirect("/me");
});
}
});
}
} catch (err) {
console.log(err);
}
});
//if got use more than 1 strategies need mention the "local" keyword for the local strategy
passport.use("local",
new Strategy(async function verify(username, password, cb) {
try {
const result = await db.query("SELECT * FROM users WHERE email = $1 ", [
username,
]);
if (result.rows.length > 0) {
const user = result.rows[0];
const storedHashedPassword = user.password;
bcrypt.compare(password, storedHashedPassword, (err, valid) => {
if (err) {
//Error with password check
console.error("Error comparing passwords:", err);
return cb(err);
} else {
if (valid) {
isLogin = true;
//update current username
username = user.username;
//update current user id
user_id = user.id;
//Passed password check
return cb(null, user);
} else {
//Did not pass password check
return cb(null, false);
}
}
});
} else {
return cb("User not found");
}
} catch (err) {
console.log(err);
}
})
);
//define Google authentication strategy
passport.use(
"google",
new GoogleStrategy (
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
//callbackURL is where Google redirects after authentication.
callbackURL: "http://localhost:3000/auth/google/me",
//userProfileURL is where Passport fetches the user's profile data.
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo", //hold user profile information
},
async (accessToken, refreshToken, profile, cb) =>{
try {
console.log("Profile", profile);
username = profile.displayName;
const result = await db.query("SELECT * FROM users WHERE email = $1;", [
profile.email,
]);
if (result.rows.length === 0) {
const newUser = await db.query(
"INSERT INTO users (username, email, password) VALUES ($1, $2, $3) RETURNING *",
[profile.displayName, profile.email, "google_oauth"]
);
console.log("New user: ", newUser.rows);
user_id = newUser.id;
isLogin = true;
return cb(null, newUser.rows[0]);
} else {
user_id = result.rows[0].id;
isLogin = true;
return cb(null, result.rows[0]);
}
} catch (err) {
return cb(err);
}
}
));
//define Facebook authentication strategy
passport.use(
"facebook",
new FacebookStrategy (
{
clientID: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
//callbackURL is where Facebook redirects after authentication.
callbackURL: "http://localhost:3000/auth/facebook/me",
},
async (accessToken, refreshToken, profile, cb) =>{
try {
console.log("Profile", profile);
username = profile.displayName;
const result = await db.query("SELECT * FROM users WHERE email = $1;", [
profile.id,
]);
if (result.rows.length === 0) {
//we store the facebook user id into the email column
const newUser = await db.query(
"INSERT INTO users (username, email, password) VALUES ($1, $2, $3) RETURNING *",
[profile.displayName, profile.id, "facebook_oauth"]
);
user_id = newUser.id;
console.log("New user: ", newUser.rows);
isLogin = true;
return cb(null, newUser.rows[0]);
} else {
user_id = result.rows[0].id;
isLogin = true;
return cb(null, result.rows[0]);
}
} catch (err) {
return cb(err);
}
}
));
passport.serializeUser((user, cb) => {
cb(null, user);
});
passport.deserializeUser((user, cb) => {
cb(null, user);
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});