-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
360 lines (324 loc) · 10.7 KB
/
index.js
File metadata and controls
360 lines (324 loc) · 10.7 KB
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
const express = require("express");
const mysql = require('mysql');
const bcrypt = require('bcrypt');
const fetch = require('node-fetch');
const saltRounds = 10;
const session = require('express-session')
const cookieParser = require("cookie-parser");
require('dotenv').config();
const app = express();
const pool = dbConnection();
// Required parameters to use session
app.set('trust proxy', 1) // trust first proxy
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: true,
cookie: { cookies: true }
}))
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(cookieParser());
//to parse Form data sent using POST method
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Uses session variables to authnticate user and make sure
// user can't access other profiles by changing the id in
// the query string
//routes
app.get('/', (req, res) => {
res.render('home')
});
// Handles sing up functionality
app.post('/signup', async (req, res) => {
let username = req.body.username;
let firstName = req.body.firstName;
let lastName = req.body.lastName;
let password = req.body.password;
let rePassword = req.body.rePassword;
// console.log(typeof(username));
let sql = `SELECT username
FROM users
WHERE username = ?`;
let rows = await executeSQL(sql, [username]);
if (username == "" || firstName == "" || lastName == "" || password == "" || rePassword == "") { //username exists in database
res.render('home', { "error": "Error: Fields Can't Be Empty" })
} else if (rows.length > 0) { //username exists in database
res.render('home', { "error": "Error: Username Already Exists" })
} else if (password != rePassword) { //pass don't match
res.render('home', { "error": "Error: Passwords Don't Match" })
} else {
// encrypt and add to table
let salt = bcrypt.genSaltSync(saltRounds);
let hash = bcrypt.hashSync(password, salt);
let sql = `INSERT INTO users
(username, firstName, lastName, password)
VALUES
(?, ?, ?, ?)`;
let rows = await executeSQL(sql, [username, firstName, lastName, hash]);
// call function that returns all of users info
rows = await getUserInfo(username);
// set auth to true
req.session.authenticated = true;
req.session.currId = rows[0].userId;
res.redirect(`/profile?id=${rows[0].userId}`)
}
});
// Handles log in functionality
app.post('/login', async (req, res) => {
let username = req.body.username;
let password = req.body.password;
let passwordHash = "";
// call function that returns all of user's info
rows = await getUserInfo(username);
// console.log(rows);
if (username == "" || password == "") {
res.render('home', { "error": "Error: Fields Can't be Empty" })
}
else if (rows.length == 0) { //username doesn't exist
res.render('home', { "error": "Error: Incorrect Username" })
} else {
passwordHash = rows[0].password;
const match = await bcrypt.compare(password, passwordHash);
// console.log(rows[0].userId);
if (match) {
req.session.authenticated = true;
req.session.currId = rows[0].userId;
res.redirect(`/profile?id=${rows[0].userId}`)
} else {
res.render('home', { "error": "Error: Wrong Credentials" })
}
}
});
app.get('/logout', isAuthenticated, (req, res) => {
req.session.destroy();
res.redirect('/');
});
app.get('/topRec', isAuthenticated, isUser, async (req, res) => {
let userId = req.query.id;
rows = await getUserInfoById(userId);
res.render('topRecs', {"user": rows})
});
app.get('/search', isAuthenticated, isUser, async (req, res) => {
let userId = req.query.id;
rows = await getUserInfoById(userId);
res.render('search', { "user": rows })
});
app.get('/myWatchlist', isAuthenticated, isUser, async (req, res) => {
let userId = req.query.id;
// gets userInfo & all anime for specific user
userInfo = await getUserInfoById(userId);
let sql = `SELECT *
FROM anime
WHERE userId = ?`;
let params = [userId];
let rows = await executeSQL(sql, params);
res.render('watchlist', { "user": userInfo, "anime": rows })
});
// Deletes specific user's anime from list
// Since each entry has unique animeId, no need to check if it's the
// right user because only that user's anime is displayed
app.post('/delFromWatchlist', isAuthenticated, isUser, async (req, res) => {
let animeId = req.body.animeId;
let userId = req.body.id;
let sql = `DELETE
FROM anime
WHERE animeId = ${animeId}`;
let rows = await executeSQL(sql);
res.redirect(`/myWatchlist?id=${userId}`)
});
// Allows users to add anime to watchlist
app.post('/addToWatchlist', isAuthenticated, isUser, async (req, res) => {
let userId = req.body.id;
let title = req.body.title;
let imgUrl = req.body.imgUrl;
let url = `${process.env.URL}/api/watchlist/${userId}&${title}`;
let response = await fetch(url);
let data = await response.json();
// if already not added to watchlist then insert
if (data.length == 0) {
// perform sql
let sql = `INSERT INTO anime
(title, userId, imgUrl)
VALUES
(?, ?, ?)`;
let params = [title, userId, imgUrl];
let rows = await executeSQL(sql, params);
}
// console.log(data);
// console.log(userId, title, imgUrl);
res.redirect(`/myWatchlist?id=${userId}`)
});
// API will be used to check if anime already added to watchlist
app.get('/api/watchlist/:id&:title', async (req, res) => {
let userId = req.params.id;
let title = req.params.title;
let sql = `SELECT title
FROM anime
WHERE userId = ? AND title = ?`;
let params = [userId, title];
let rows = await executeSQL(sql, params);
// when use api use send
res.send(rows);
});
app.get('/profile', isAuthenticated, isUser, async (req, res) => {
let userId = req.query.id;
rows = await getUserInfoById(userId);
res.render('profile', { "user": rows })
});
// Updates user's info
app.post('/profile', isAuthenticated, isUser, async (req, res) => {
let userId = req.body.id;
let fName = req.body.firstName;
let lName = req.body.lastName;
rows = await getUserInfoById(userId);
// console.log(userId, fName, lName);
if (fName == "" || lName == "") {
res.render('profile', { "user": rows, "error": "Error: Fields Can't be Empty" })
} else {
// UPDATES Info User Inputted
let sql = `UPDATE users
SET
firstName = ?,
lastName = ?
WHERE
userId = ?`;
let params = [fName, lName, userId];
rows = await executeSQL(sql, params);
rows = await getUserInfoById(userId);
res.render('profile', { "user": rows })
}
});
// Updates user's password
app.post('/updatePass', isAuthenticated, isUser, async (req, res) => {
let userId = req.body.id;
let password = req.body.password;
rows = await getUserInfoById(userId);
// console.log(userId, fName, lName);
if (password == "") {
res.render('profile', { "user": rows, "error2": "Error: Field Can't be Empty" })
} else {
// UPDATES password
// encrypt new password
let salt = bcrypt.genSaltSync(saltRounds);
let hash = bcrypt.hashSync(password, salt);
let sql = `UPDATE users
SET
password = ?
WHERE
userId = ?`;
let params = [hash, userId];
rows = await executeSQL(sql, params);
rows = await getUserInfoById(userId);
// console.log(password);
// console.log(hash);
res.render('profile', { "user": rows })
}
});
app.get('/userReviews', isAuthenticated, isUser, async (req, res) => {
let userId = req.query.id;
// gets userInfo & all anime for specific user
userInfo = await getUserInfoById(userId);
let sql = `SELECT *
FROM reviews r
INNER JOIN users u
ON r.userId = u.userId`;
let rows = await executeSQL(sql);
// console.log(userInfo);
res.render('userReviews', { "user": userInfo, "reviews": rows})
});
// Deletes specific user's review
app.post('/delReview', isAuthenticated, isUser, async (req, res) => {
let reviewId = req.body.reviewId;
let userId = req.body.id;
let sql = `DELETE
FROM reviews
WHERE reviewId = ${reviewId}`;
let rows = await executeSQL(sql);
res.redirect(`/userReviews?id=${userId}`)
});
// Allows users to add reviews
app.post('/addReview', isAuthenticated, isUser, async (req, res) => {
let userId = req.body.id;
let title = req.body.title;
let rating = req.body.rating;
let review = req.body.review;
// If either one empty, then display error message and re-render
if(title == "" || rating == "" || review == "")
{
userInfo = await getUserInfoById(userId);
let sql = `SELECT *
FROM reviews r
INNER JOIN users u
ON r.userId = u.userId`;
let rows = await executeSQL(sql);
// console.log(userInfo);
res.render('userReviews', {"user": userInfo, "reviews": rows, "error": "Error: Fields Can't Be Empty"})
}else{
// Insert input to reviews table
let sql = `INSERT INTO reviews
(userId, description, rating, title)
VALUES
(?, ?, ?, ?)`;
let rows = await executeSQL(sql, [userId, review, rating, title]);
// redirect
res.redirect(`/userReviews?id=${userId}`)
}
});
async function getUserInfo(username) {
let sql = `SELECT *
FROM users
WHERE username = ?`;
let rows = await executeSQL(sql, [username]);
return rows
}
async function getUserInfoById(userId) {
let sql = `SELECT *
FROM users
WHERE userId = ?`;
let rows = await executeSQL(sql, [userId]);
return rows
}
function isAuthenticated(req, res, next) {
if (req.session.authenticated) {
next();
} else {
res.render('home')
}
}
function isUser(req, res, next) {
if (req.session.currId == req.query.id || req.session.currId == req.body.id) {
next();
} else {
res.send("ERROR: Cannot access page")
}
}
app.get("/dbTest", async function(req, res) {
let sql = "SELECT CURDATE()";
let rows = await executeSQL(sql);
res.send(rows);
});//dbTest
//functions
async function executeSQL(sql, params) {
return new Promise(function(resolve, reject) {
pool.query(sql, params, function(err, rows, fields) {
if (err) throw err;
resolve(rows);
});
});
}//executeSQL
//values in red must be updated
function dbConnection() {
const pool = mysql.createPool({
connectionLimit: 10,
host: process.env.DB_HOSTNAME,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});
return pool;
} //dbConnection
//start server
app.listen(3000, () => {
console.log("Expresss server running...")
})