-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
391 lines (362 loc) · 12 KB
/
server.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
const express = require("express");
const app = express();
const db = require("./db");
const cookieSession = require("cookie-session");
const bcrypt = require("./bcrypt");
//____________SET UP HANDLEBARS________________//
const { engine } = require("express-handlebars");
app.engine("handlebars", engine());
app.set("view engine", "handlebars");
// access to public folder
app.use(express.static("./public"));
//express.urlencoded for parsing your form POST request
app.use(
express.urlencoded({
extended: false,
})
);
const COOKIE_SECRET =
process.env.COOKIE_SECRET || require("./secrets.json").COOKIE_SECRET;
// middleware to implement the tamper proof cookie session
app.use(
cookieSession({
secret: COOKIE_SECRET,
maxAge: 1000 * 60 * 60 * 24 * 14, // keeps cookie for this amout of time
})
);
app.use((req, res, next) => {
console.log("--------------------------");
console.log(req.url);
console.log(req.method);
console.log(req.session);
console.log("--------------------------");
next();
});
app.get("/", (req, res) => {
res.redirect("/register");
});
// get the register template
app.get("/register", (req, res) => {
if (req.session.signatureId) {
return res.redirect("/thanks");
} else if (req.session.user_id) {
return res.redirect("/petition");
}
res.render("register", {
title: "Register",
});
});
// app.post("register")
app.post("/register", (req, res) => {
// call the bcrypt.hash function and pass it the password from req.body
bcrypt
.hash(req.body.password)
.then((hash) => {
db.addUser(req.body.first, req.body.last, req.body.email, hash)
.then((results) => {
req.session.user_id = results.rows[0].id;
req.session.login = true;
//redirect to profile page
res.redirect("/profile");
})
.catch((err) => {
res.render("register", {
title: "Register",
error: true,
});
});
})
.catch((err) => {
console.log("err /register", err);
});
// call a function to insert the hashed password that bcrypt.hash returned plus the first, last, and
// email from req.body into the database and create a new user
// after the query, put the newly created user's id into the session so that the user is logged in.
// Any time you want to check to see if a user is logged in you can check to see if req.session.userId exists
});
app.get("/login", (req, res) => {
if (!req.session.user_id) {
res.render("login", {
title: "login",
});
} else {
res.redirect("/petition");
}
});
app.post("/login", (req, res) => {
db.login(req.body.email)
.then((results) => {
//console.log(results.rows[0].password);
if (results.rows[0]) {
return bcrypt
.compare(req.body.password, results.rows[0].password)
.then(function (pwCompare) {
if (pwCompare) {
req.session.login = true;
req.session.user_id = results.rows[0].id;
db.findSignature(req.session.user_id)
.then((results) => {
if (results.rows[0]) {
req.session.signed = true;
req.session.signatureId =
results.rows[0].id;
res.redirect("/thanks");
} else {
res.redirect("/petition");
}
})
.catch((err) => {
console.log("error sig", err);
});
} else {
res.render("login", {
title: "login",
error: true,
});
}
});
} else {
res.render("login", {
title: "login",
error: true,
});
}
})
.catch((err) => {
console.log("err in login", err);
res.render("login", {
title: "Login",
error: true,
});
});
});
///_____PROFILE_____///
app.get("/profile", (req, res) => {
if (!req.session.user_id) {
return res.redirect("/register");
}
res.render("profile", {
title: "Profile",
});
});
function isInputEmpty(obj) {
for (let key in obj) {
if (obj[key].trim().length != 0) {
return false;
}
}
return true;
}
app.post("/profile", (req, res) => {
// check to see that the user entered data into at least one of the fields.
// if they are empty:
if (isInputEmpty(req.body)) {
//we keep going but we dont add somthing in the Db.
//return;
return res.redirect("/petition");
}
// if they are not empty:
console.log("req.body.url.trim().length ", req.body.url.trim().length);
let url = req.body.url.trim().length != 0 ? req.body.url.trim() : null;
const age = req.body.age || null;
const city = req.body.city || null;
//newProfile = (age, city, user_id, url)
db.newProfile(age, city, req.session.user_id, url)
.then((results) => {
res.redirect("petition");
})
.catch((err) => {
console.log("error in [db]newProfile");
res.render("profile", {
title: "Profile",
error: true,
});
});
});
///____SIGN-PETITION____///
app.get("/petition", (req, res) => {
if (!req.session.user_id) {
return res.redirect("/register");
} else if (req.session.signatureId) {
res.redirect("/thanks");
} else {
res.render("petition", {
title: "Petition",
});
}
});
app.post("/petition", (req, res) => {
db.addSigniture(req.body.signature, req.session.user_id)
.then((results) => {
req.session.signatureId = results.rows[0].id;
req.session.signed = true;
console.log("omg it worked");
// console.log("results", results.rows[0].id);
// currently not showing up oben richtiger name
res.redirect("/thanks");
})
.catch((err) => {
console.log("error in [db]addSigniture", err);
res.render("petition", {
title: "Petition",
error: true,
});
});
});
// AB HIER COOKIES CHECKEN
app.get("/thanks", function (req, res) {
if (!req.session.signatureId) {
return res.redirect("/petition");
}
db.getSignitureId(req.session.signatureId).then((results) => {
const signatureURL = results.rows[0].url;
db.countSignatures().then((results) => {
const numSign = results.rows[0].count;
res.render("thanks", {
data: {
url: signatureURL,
numSign,
},
});
});
});
});
// DELETE SIGNATURE
app.post("/thanks", (req, res) => {
db.deleteSignature(req.session.signatureId)
.then((results) => {
req.session.signed = false;
req.session.signatureId = null;
res.redirect("/petition");
})
.catch((err) => {
console.log("error in delete sign", err);
});
});
//____SIGNERS-PAGE__________
// getting all of users and user-profiles info on the page
app.get("/signers", function (req, res) {
if (!req.session.signatureId) {
return res.redirect("/petition");
}
db.getSignersInput(
req.body.first,
req.body.last,
req.body.age,
req.body.city,
req.body.url
).then((results) => {
let signer = results.rows;
res.render("signers", {
title: "signer",
signer,
});
});
});
//______SIGNERS/:CITY
app.get("/signers/:city", (req, res) => {
let city = req.params.city;
// db get signers by city
console.log("req.params", req.params.city);
db.getCity(city)
.then((results) => {
const resultCity = results.rows;
res.render("signers", {
title: "signers",
signer: resultCity,
city: resultCity[0].city,
});
})
.catch((err) => console.log("err", err));
});
///______EDIT-PROFILE_______
app.get("/edit", (req, res) => {
if (req.session.user_id) {
db.getUserInfo(req.session.user_id)
.then((results) => {
res.render("edit", {
title: "Edit",
userInfo: results.rows[0],
});
})
.catch((err) => {
console.log("error getUserInfo", err);
});
} else {
return res.redirect("/register");
}
});
// notes from class
app.post("/edit", (req, res) => {
if (req.body.password === "") {
db.editUsersInfoNoPassword(
req.body.first,
req.body.last,
req.body.email,
req.session.user_id
)
.then(() => {
db.upsertProfileInfo(
req.body.age,
req.body.city,
req.body.url,
req.session.user_id
)
.then(() => {
res.redirect("/thanks");
})
.catch((err) => {
console.log("error in upsertProfileInfo NoPW", err);
});
})
.catch((err) => {
console.log("error in EDIT NO PW", err);
});
} else {
bcrypt
.hash(req.body.password)
.then((hash) => {
db.editUsersInfoPassword(
req.body.first,
req.body.last,
req.body.email,
hash,
req.session.user_id
)
.then(() => {
db.upsertProfileInfo(
req.body.age,
req.body.city,
req.body.url,
req.session.user_id
)
.then(() => {
res.redirect("/thanks");
})
.catch(function (err) {
console.log(err);
});
})
.catch((err) => {
res.render("edit", {
title: "edit",
error: true,
});
});
})
.catch((err) => {
console.log("err /register", err);
});
}
});
// LOGOUT
app.get("/logout", (req, res) => {
req.session = null;
res.redirect("/login");
});
//instead of using hard coded 8080 pick up from process.env.PORT
// check if PORT is provided IF NOT run on 8080
app.listen(process.env.PORT || 8080, () =>
console.log("go got this petition stuff...")
);
//PORT=8080 node server.js