forked from yuukiok/social-chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
389 lines (332 loc) · 10.8 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
const express = require('express');
const mysql = require('mysql');
const app = express();
const bcrypt = require('bcrypt');
const session = require('express-session');
const port = 3000;
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
const pool = dbConnection();
const fetch = require('node-fetch');
const e = require('express');
// for environment file
require('dotenv').config();
app.set('trust proxy', 1); // trust first proxy
const option = {
secret: 'random ch@r@ct3rs',
resave: false,
saveUninitialized: false,
cookie: { maxAge: 60 * 60 * 1000 },
};
app.use(session(option));
app.use(express.json());
// middleware function
const isAuthenticated = (req, res, next) => {
if (req.session.userId != undefined) {
next();
} else {
res.redirect('/');
}
};
app.get('/', (req, res) => {
if (req.session.userId != undefined) {
res.redirect('/home');
} else {
res.render('login');
}
});
// [login page] (POST /login)
app.post('/login', async (req, res) => {
let username = req.body.username;
let password = req.body.password;
let passwordHash = '';
const sql = ` SELECT userID, pWord
FROM users
WHERE username = ?`;
let rows = await executeSQL(sql, username);
if (rows.length > 0) {
passwordHash = rows[0].pWord;
}
const match = await bcrypt.compare(password, passwordHash);
if (match) {
req.session.userId = rows[0].userID;
res.redirect('/home');
} else {
res.redirect('/');
}
});
// [create/sign in] (POST /create)
app.post('/create', async (req, res) => {
let username = req.body.username;
let password = req.body.password;
let firstName = req.body.firstName;
let lastName = req.body.lastName;
let country = req.body.country;
let retype = req.body.re_password;
if (password != retype) {
res.render('create', { error: 'Passwords do not match!' });
}
let hash = bcrypt.hashSync(password, 10);
// validation for duplicate user
let sql_user = `SELECT username
FROM users`;
let user_row = await executeSQL(sql_user);
for (i = 0; i < user_row.length; i++) {
if (user_row[i].username == username) {
console.log('error_create');
res.render('create', { error: 'Account has already exsisted!' });
}
}
let sql = `INSERT INTO users
(username, pWord, firstName, lastName, country)
VALUES
(?, ?, ?, ?, ?)`;
let params = [username, hash, firstName, lastName, country];
let rows = await executeSQL(sql, params);
res.render('login');
});
app.get('/create', (req, res) => {
res.render('create', { error: '' });
});
// [home page] (GET /home)
app.get('/home', isAuthenticated, async (req, res) => {
let sql = `Select * from cuisines`;
let cuisines = await executeSQL(sql);
let userID = req.session.userId;
sql = `select firstName from users where userID = ?`;
let fName = await executeSQL(sql, userID);
let dailyCall = `https://api.edamam.com/api/recipes/v2?type=public&q=chicken&app_id=${process.env.API_ID}&app_key=${process.env.API_KEY}`;
let response = await fetch(dailyCall);
let daily = await response.json();
res.render('home', { cuisines: cuisines, daily: daily, firstN: fName });
});
app.get('/homeSearch', isAuthenticated, async (req, res) => {
// Grabbing the info from the form in home page
let keyword = req.query.keyword;
let cuisineType = req.query.cuisine;
let mealTime = req.query.time;
if (mealTime == undefined) {
mealTime = 'lunch';
}
// puting that info into the api url and turning the response into json
let apiCall = `https://api.edamam.com/api/recipes/v2?type=public&q=${keyword}&app_id=${process.env.API_ID}&app_key=${process.env.API_KEY}&cuisineType=${cuisineType}&mealType=${mealTime}`;
let response = await fetch(apiCall);
let recipes = await response.json();
let dailyCall = `https://api.edamam.com/api/recipes/v2?type=public&q=chicken&app_id=${process.env.API_ID}&app_key=${process.env.API_KEY}`;
let dayRes = await fetch(dailyCall);
let daily = await dayRes.json();
// grabbing the cuisines from the database
let sql = `Select * from cuisines`;
let cuisines = await executeSQL(sql);
let userID = req.session.userId;
sql = `select firstName from users where userID = ?`;
let fName = await executeSQL(sql, userID);
//passing the data onto the home page from the db and api call
res.render('home', {
cuisines: cuisines,
recipes: recipes,
daily: daily,
firstN: fName,
});
});
// [settings] (GET /userInfo)
app.get('/settings', isAuthenticated, async (req, res) => {
let sql = ` SELECT *
FROM users
WHERE userId = ?`;
let params = req.session.userId;
let data = await executeSQL(sql, [params]);
let userID = req.session.userId;
sql = `select firstName from users where userID = ?`;
let fName = await executeSQL(sql, userID);
res.render('settings', { data: data[0], firstN: fName});
});
// [add/update settings] (POST /userInfo)
app.post('/settings', isAuthenticated, async (req, res) => {
let username = req.body.username;
let firstName = req.body.firstName;
let lastName = req.body.lastName;
let country = req.body.country;
let sql = `UPDATE users
SET
username = ?,
firstName = ?,
lastName = ?,
country = ?
WHERE userId = ?`;
let params = [username, firstName, lastName, country, req.session.userId];
await executeSQL(sql, params);
res.redirect('settings');
});
app.post('/password', isAuthenticated, async (req, res) => {
let old_password = req.body.old_password;
let new_password = req.body.new_password;
let userId = req.session.userId;
let sql = ` SELECT pWord
FROM users
WHERE userId = ?`;
let data = await executeSQL(sql, [userId]);
let passwordHash = data[0].pWord;
let match = await bcrypt.compare(old_password, passwordHash);
if (match) {
let hash = bcrypt.hashSync(new_password, 10);
let updateSql = ` UPDATE users
SET
pWord = ?
WHERE userId = ?`;
let params = [hash, userId];
await executeSQL(updateSql, params);
}
res.redirect('settings');
});
// [new recipe] has input form (GET /recipe)
app.get('/newRecipe', isAuthenticated, async (req, res) => {
let sql = `Select * from cuisines`;
let cuisines = await executeSQL(sql);
let userID = req.session.userId;
sql = `select firstName from users where userID = ?`;
let fName = await executeSQL(sql, userID);
res.render('newRecipe', { cuisines: cuisines, firstN: fName});
});
// [add recipes] in your own (use form from scrach without api) (POST /recipe)
app.post('/addRecipe', isAuthenticated, async (req, res) => {
let name = req.body.name;
let calories = req.body.calories;
let servingSize = req.body.servingSize;
let cuisine = req.body.cuisine;
let mealTime = req.body.time;
let url = req.body.url;
let img = req.body.img;
let userId = req.session.userId;
let sql = `INSERT INTO recipes
(user_id,
recipe_name,
cuisine,
calories,
serving_size,
meal_time,
recipe_link,
image_link)
VALUES
(?,?,?,?,?,?,?,?)`;
let params = [
userId,
name,
cuisine,
calories,
servingSize,
mealTime,
url,
img,
];
let rows = await executeSQL(sql, params);
res.redirect('newRecipe');
});
// [save recipes] from api (GET /savedRecipes)
app.post('/saveRecipe', isAuthenticated, async (req, res) => {
// SQL QUERY TO INSERT DATA INTO DATABASE
const sql = `INSERT INTO recipes
(user_Id, recipe_name, cuisine, calories, serving_size, meal_time, recipe_link, image_link)
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`;
// RECIPE OBJECT WITH THE DATA PASSED THROUGH
let recipe = {
userId: req.session.userId,
name: req.body.name,
calories: req.body.calories,
yield: req.body.servings,
time: req.body.mealTime,
cuisine: req.body.cuisine,
recLink: req.body.link,
img: req.body.img,
};
// ARRAY TO PASS THOSE VALUES INTO SQL QUERY TO AVOID INJECTION
let params = [
recipe.userId,
recipe.name,
recipe.cuisine,
recipe.calories,
recipe.yield,
recipe.time,
recipe.recLink,
recipe.img,
];
// INSERT AND REDIRECT TO HOME
let rows = await executeSQL(sql, params);
res.redirect('/home');
});
app.get('/saved', isAuthenticated, async (req, res) => {
let userID = req.session.userId;
let sql = `SELECT * FROM recipes WHERE user_id = ?`;
let recipe = await executeSQL(sql, userID);
sql = `select firstName from users where userID = ?`;
let fName = await executeSQL(sql, userID);
res.render('saved', { recipes: recipe, user: userID, firstN: fName });
});
// [Unsave recipes] from api (POST /saved)
app.get('/UnsaveRecipe', isAuthenticated, async (req, res) => {
let userID = req.session.userId;
let params = [req.query.recipeId, userID];
let unSaveRecipe = `DELETE FROM recipes
WHERE recipe_id = ? AND user_id = ?`;
await executeSQL(unSaveRecipe, params);
res.redirect('/saved');
});
// [new recipe] has input form (GET /recipe)
app.get('/addRecipe', isAuthenticated, (req, res) => {
res.render('newRecipe');
});
// [logout] (GET /login)
app.get('/logout', (req, res) => {
req.session.destroy();
res.redirect('/');
});
app.get('/poke', async (req, res) => {
let userID = req.session.userId;
sql = `select firstName from users where userID = ?`;
let fName = await executeSQL(sql, userID);
res.render('poke', {firstN: fName});
});
app.get('/poke/search', async (req, res) => {
let pokeId = req.query.id;
if (pokeId === 0) {
pokeId++;
}
if (pokeId == '') {
pokeId = 201;
}
if (pokeId > 649) {
pokeId = 649;
}
userID = req.session.userId;
sql = `select firstName from users where userID = ?`;
let fName = await executeSQL(sql, [userID]);
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokeId}/`);
const data = await response.json();
res.render('poke', {firstN: fName, pokeInfo: req.query, data: data });
});
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,
connectTimeout: 60 * 60 * 1000,
acquireTimeout: 60 * 60 * 1000,
timeout: 60 * 60 * 1000,
host: 'h1use0ulyws4lqr1.cbetxkdyhwsb.us-east-1.rds.amazonaws.com',
user: 'e7lupxcx8d4xn9t6',
password: 'cay2rck66m43hje5',
database: 'ejes6a2uewb3lyp4',
});
return pool;
} //dbConnection
app.listen(port, () => {
console.log(`Example app listening on port http://localhost:${port}`);
});