-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
251 lines (220 loc) · 5.95 KB
/
server.js
File metadata and controls
251 lines (220 loc) · 5.95 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
const express = require("express");
const app = express();
const db = require("./models");
const crypto = require("crypto");
const pbkdf2 = require("pbkdf2");
const session = require("express-session");
const handlebars = require("express-handlebars");
const { Op } = require("sequelize");
app.use(
session({
secret: "Windward",
resave: true,
saveUninitialized: true,
cookie: { maxAge: 60 * 60 * 1000 },
})
);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Set Handlebars as view engine
app.set("view engine", "hbs");
app.engine(
"hbs",
handlebars({
layoutsDir: __dirname + "/views/layouts",
extname: "hbs",
defaultLayout: "index",
partialsDir: __dirname + "/views/partials",
})
);
// Serve Static css/image files
app.use(express.static("public"));
// Encryption
function encryptPassword(password, pass_salt) {
var salt = pass_salt ? pass_salt : crypto.randomBytes(20).toString("hex");
var key = pbkdf2.pbkdf2Sync(password, salt, 36000, 256, "sha256");
var hash = key.toString("hex");
return `$${salt}$${hash}`;
}
function isAuthenticated(req, res, next) {
if (req.session.user) {
next();
} else res.redirect("/login");
}
// ------ ROUTES --------
app.get("/school/:id", isAuthenticated, (req, res) => {
db.highschool.findOne({ where: { id: req.params.id } }).then((results) => {
let school = results.dataValues;
let schoolID = school.id;
// console.log(schoolID);
db.thread
.findAll({ where: { highschool_id: schoolID } })
.then((results) => {
let threads = results.map((thread) => thread.toJSON());
console.log(threads);
console.log(req.session.user);
res.render("schoolpage", {
user: req.session.user,
highschool: school,
thread: threads,
});
});
});
});
app.post("/school/:id/thread", isAuthenticated, (req, res) => {
db.thread
.create({
highschool_id: req.params.id,
title: req.body.title,
content: req.body.content,
user_id: req.body.user_id,
})
.then(() => {
res.redirect(`/school/${req.params.id}`);
});
});
app.get("/school/:id/thread/:thread", isAuthenticated, (req, res) => {
// console.log(req.params);
// console.log("session" + req.session);
db.thread
.findOne({ where: { highschool_id: req.params.id, id: req.params.thread } })
.then((results) => {
if(req.session.user.id == results.dataValues.user_id) {
results.destroy();
res.redirect(`/school/${req.params.id}`);
} else {
res.status(401).send("not authorized to delete other users posts")
}
// console.log(results)
});
});
app.post("/update-password", isAuthenticated, (req, res) => {
// console.log(req.body);
db.user
.update(
{ password: encryptPassword(req.body.password) },
{ where: {
id: req.body.user_id,
},
}
)
.then((user) => {
res.redirect("/login");
});
});
app.get("/update-password", isAuthenticated, (req, res) => {
res.render("updatepassword", {
user: req.session.user,
active: { search: true },
});
});
// Main Page Routes
app.get("/", (req, res) => {
res.render("home", {
user: req.session.user,
active: { home: true },
});
});
app.get("/login", (req, res) => {
res.render("login", {
user: req.session.user,
active: { login: true },
});
});
app.get("/search", isAuthenticated, (req, res) => {
res.render("search", {
user: req.session.user,
active: { search: true },
});
});
// Sign-Up Routes
app.get("/sign-up", (req, res) => {
if (req.session.user) {
res.redirect("/");
} else res.render("sign-up");
});
app.post("/sign-up", (req, res) => {
// console.log(req.body);
if (req.body.username && req.body.password) {
db.user
.create({
username: req.body.username,
password: encryptPassword(req.body.password),
})
.then((user) => {
req.session.user = user;
res.redirect("/search");
// res.redirect("/login");
});
} else {
res.send(" please send username and password.");
}
});
// Search Route
app.get("/api/search/:name", isAuthenticated, (req, res) => {
let schoolName = req.params.name;
db.highschool
.findAll({
where: {
name: {
[Op.iLike]: `%${schoolName}%`,
},
},
})
.then((results) => {
if (results !== undefined && results.length != 0) {
// console.log(results);
schools = results.map((school) => school.toJSON());
console.log(schools);
res.json(schools);
} else {
res.status(404).json(`No School found matching ${schoolName}`);
}
});
});
// GET All schools
app.get("/api/school", isAuthenticated, function (request, response, next) {
db.highschool.findAll().then((results) => {
res.send(results);
});
response.send();
});
// Login Routes
app.get("/logout", (req, res) => {
req.session.destroy();
res.redirect("/");
});
app.post("/login", (req, res) => {
if (req.body.username && req.body.password) {
db.user
.findOne({
where: {
username: req.body.username,
},
})
.then((user) => {
if (user) {
var pass_parts = user.password.split("$");
// encrypt req.body.password using pass_parts[1]
let encryptedPass = encryptPassword(req.body.password, pass_parts[1]);
// compared hashed password with user password
if (encryptedPass == user.password) {
req.session.user = user;
res.redirect("/search");
} else {
res.send("wrong password");
}
} else {
res.send("we could not find any users");
}
})
.catch(() => {
res.status(401).send("There was an error");
});
} else {
res.send("please send a username and password");
}
});
app.listen(process.env.PORT || 3000, function () {
console.log("listening in port 3000");
});