-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
300 lines (275 loc) · 8.89 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
const express = require("express");
const app = express();
const port = process.env.PORT || 8080;
const db = require("./utils/db");
const hb = require("express-handlebars");
const cookieSession = require("cookie-session");
const { hash, compare } = require("./utils/bc");
const csurf = require("csurf");
const {
requireLoggedOutUser,
requireNoSignature,
requireSignature,
makeCookiesSafe,
requireLoggedInUser
} = require("./middleware");
app.engine("handlebars", hb());
app.set("view engine", "handlebars");
app.use(
express.urlencoded({
extended: false
})
);
app.use(express.static("./public"));
app.use(
cookieSession({
secret: `The owls are not what they seem.`,
maxAge: 1000 * 60 * 60
})
);
app.use(csurf());
app.use(makeCookiesSafe);
app.use(requireLoggedInUser);
app.get("/", (req, res) => {
res.redirect("/register");
});
app.get("/petition", requireNoSignature, (req, res) => {
console.log("User ID: ", req.session.userId);
res.render("petition", {});
});
app.post("/petition", requireNoSignature, (req, res) => {
const { signature } = req.body;
db.addSignature(req.session.userId, signature)
.then(result => {
req.session.signed = true;
console.log(result.rows[0]);
console.log(
"Signature posted successfully! User id: ",
result.rows[0].user_id
);
res.redirect("/thanks");
})
.catch(err => {
console.log("Signature post error: ", err);
res.render("petition", {
error: err
});
});
});
app.get("/thanks", requireSignature, (req, res) => {
let numSigners;
db.getSignatures()
.then(result => {
numSigners = result.rows.length;
return db.getSignaturebyUserId(req.session.userId);
})
.then(result => {
const signer = result.rows[0];
console.log("Signer: ", signer);
res.render("thanks", {
signer,
numSigners
});
})
.catch(err => {
console.log("Error in thank you page: ", err);
});
});
app.post("/thanks", (req, res) => {
db.deleteSignature(req.session.userId).then(() => {
req.session.signed = false;
res.redirect("/petition");
});
});
app.get("/signers", requireSignature, (req, res) => {
db.getSigners()
.then(result => {
console.log("signers: ", result.rows);
const signers = result.rows;
for (let i = 0; i < signers.length; i++) {
if (signers[i].city != null) {
signers[i].urlCity = signers[i].city;
signers[i].cityName = signers[i].city.replace(/_/g, " ");
}
}
res.render("signers", { signers });
})
.catch(err => {
res.render("signers", { error: err });
});
});
app.get("/signers/:city", requireSignature, (req, res) => {
let city = req.params.city.replace(/_/g, " ");
console.log("req.params.city: ", req.params.city);
console.log("City in URL: ", city);
db.getSignersByCity(req.params.city)
.then(result => {
const signers = result.rows;
res.render("city-signers", { signers, city });
})
.catch(err => {
res.render("city-signers", { error: err });
});
});
app.get("/register", requireLoggedOutUser, (req, res) => {
res.render("register", {});
});
app.post("/register", requireLoggedOutUser, (req, res) => {
// grab user input and read it on the server
const { first, last, email, password } = req.body;
// hash the password that the user typed and THEN
hash(password)
.then(hashedPw => {
console.log("hashedPw: ", hashedPw);
// insert a row in the USERS table
return db.addUser(first, last, email, hashedPw);
})
.then(result => {
// if insert is successful, add userId in a cookie
req.session.userId = result.rows[0].id;
console.log("User ID: ", req.session.userId);
res.redirect("/profile");
})
// if insert fails, re-render template with an error message
.catch(err => {
res.render("register", { error: err });
});
});
app.get("/login", requireLoggedOutUser, (req, res) => {
res.render("login", {});
});
app.post("/login", requireLoggedOutUser, (req, res) => {
const { email, password } = req.body;
console.log("Entered email address: ", email);
let id;
// get the user's stored hashed password from the db using the user's email address
db.getUserByEmail(email)
.then(result => {
console.log("User: ", result.rows);
if (result.rows[0].signature) {
req.session.signed = true;
}
id = result.rows[0].id;
// pass the hashed password to COMPARE along with the password the user typed in the input field
return compare(password, result.rows[0].password);
})
.then(result => {
console.log("result of compare: ", result);
// if they match, COMPARE returns a boolean value of true
if (result) {
// store the userId in a cookie
req.session.userId = id;
res.redirect("/petition");
} else {
// if they don't match, COMPARE returns a boolean value of false and re-render with error message
res.render("login", {
error: "Password incorrect. Please try again."
});
}
})
.catch(err => {
console.log("Login error: ", err);
res.render("login", { error: err });
});
});
app.get("/logout", (req, res) => {
req.session = null;
res.redirect("/login");
});
app.get("/profile", (req, res) => {
res.render("profile", {});
});
app.post("/profile", (req, res) => {
let { age, city, homepage } = req.body;
if (age == "") {
age = null;
}
if (homepage && !homepage.startsWith("http")) {
res.render("profile", {
error: "Homepage url must start with http:// or https://"
});
} else {
//remove spaces from city
city = city.replace(/\s/g, "_");
db.addProfile(age, city, homepage, req.session.userId)
.then(result => {
console.log("User profile: ", result.rows);
res.redirect("/petition");
})
.catch(err => {
res.render("profile", { error: err });
});
}
});
app.get("/profile/edit", (req, res) => {
db.getUserProfile(req.session.userId).then(result => {
const user = result.rows[0];
if (user.city) {
user.city = user.city.replace(/_/g, " ");
}
console.log("User: ", user);
res.render("profile-edit", { user });
});
});
app.post("/profile/edit", (req, res) => {
let { first, last, email, password, age, city, homepage } = req.body;
//remove spaces from city
city = city.replace(/\s/g, "_");
if (age == "") {
age = null;
}
if (homepage && !homepage.startsWith("http")) {
res.render("profile", {
error: "Homepage url must start with http:// or https://"
});
}
if (password) {
// hash the password!!
hash(password)
.then(hashedPw => {
// update 4 columns in users
return db.updateUserAndPassword(
first,
last,
email,
hashedPw,
req.session.userId
);
})
.then(() => {
// upsert user_profiles
return db.upsertUserProfile(
age,
city,
homepage,
req.session.userId
);
})
.then(() => {
res.redirect("/petition");
})
.catch(err => {
console.log("Error updating user profile; ", err);
res.render("profile-edit", { error: err });
});
} else {
// update 3 columns in users
db.updateUser(first, last, email, req.session.userId)
.then(() => {
return db.upsertUserProfile(
age,
city,
homepage,
req.session.userId
);
})
.then(() => {
res.redirect("/petition");
})
.catch(err => {
console.log("Error updating user profile: ", err);
res.render("profile-edit", { error: err });
});
}
});
app.listen(port, () => console.log(`Petition up and running on ${port}`));