-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress_server.js
275 lines (254 loc) · 7.46 KB
/
express_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
const express = require('express');
const cookieSession = require('cookie-session');
const morgan = require('morgan');
const bodyParser = require("body-parser");
const bcrypt = require('bcrypt');
const { getUserByEmail, generateRandomString } = require('./helpers');
const app = express();
const PORT = 8080;
app.set("view engine", "ejs");
app.use(morgan('dev'));
app.use(cookieSession({ name: 'session', keys: ['my secret key', 'another secret key'] }));
app.use(bodyParser.urlencoded({ extended: true }));
const urlDatabase = {
b6UTxQ: {
longURL: "https://www.tsn.ca",
userID: "123asdd"
},
i3BoGr: {
longURL: "https://www.google.ca",
userID: "323s3s"
}
};
const users = {
"123asdd": {
id: "123asdd",
email: "[email protected]",
//password: 123456
password: "$2b$10$U7VttdTfFUAUmTR5SIkeqe/4.TAldDnP/hDH7bKWfzl543HChyHPC"
},
"323s3s": {
id: "323s3s",
email: "[email protected]",
// password: 1234567
password: "$2b$10$OwBYIQ9m.XOGWOB12/pT/.x9/kSy7ihWU9MKwzBnX.wmWE.Y4hwvi"
}
};
//home page **** GET / ****
app.get("/", (req, res) => {
if (!users[req.session.user_id]) {
res.redirect('/login');
}
const userID = users[req.session.user_id].id;
const templateVars = {
urls: urlDatabase,
user: users[req.session.user_id],
userID
};
res.render('urls_index', templateVars);
});
// **** GET /urls ****
app.get("/urls", (req, res) => {
if (!users[req.session.user_id]) {
const errorMsg = { message: 'You dont have permission to visit this page, please login or register!', code: 403 };
res.status(403);
res.render('error', errorMsg);
return;
}
const userID = users[req.session.user_id].id;
const templateVars = {
urls: urlDatabase,
user: users[req.session.user_id],
userID
};
res.render('urls_index', templateVars);
});
//**** GET /urls/new ****
app.get("/urls/new", (req, res) => {
if (!users[req.session.user_id]) {
res.redirect('/login');
}
const templateVars = {
urls: urlDatabase,
user: users[req.session.user_id]
};
res.render("urls_new", templateVars);
});
//**** GET /urls/:id ****
app.get("/urls/:shortURL", (req, res) => {
const shortURL = req.params.shortURL;
if (!urlDatabase[shortURL]) {
const errorMsg = { message: 'The short URL not found!!', code: 404 };
res.status(404);
res.render('error', errorMsg);
return;
}
if (!users[req.session.user_id]) {
const errorMsg = { message: 'Please login or register, You dont have permission to visit this page!!!', code: 403 };
res.status(403);
res.render('error', errorMsg);
return;
}
if (users[req.session.user_id].id !== urlDatabase[shortURL].userID) {
const errorMsg = { message: 'This is not your short URL, you dont have permission to edit!', code: 403 };
res.status(403);
res.render('error', errorMsg);
return;
}
const longURL = urlDatabase[shortURL].longURL;
const templateVars = {
shortURL,
longURL,
user: users[req.session.user_id]
};
res.render('urls_show', templateVars);
});
// **** GET /u/:id ****
app.get("/u/:shortURL", (req, res) => {
if (!urlDatabase[req.params.shortURL]) {
const errorMsg = { message: 'Page not found!', code: 404 };
res.status(403);
res.render('error', errorMsg);
return;
}
const longURL = urlDatabase[req.params.shortURL].longURL;
res.redirect(longURL);
});
// *** POST /urls ***
app.post("/urls", (req, res) => {
const shortURL = generateRandomString();
const longURL = req.body.longURL;
const user = req.session.user_id;
if (!user) {
const errorMsg = { message: 'Please login first!!', code: 400 };
res.status(400);
res.render('error', errorMsg);
return;
}
urlDatabase[shortURL] = {
longURL: longURL, userID: user
};
res.redirect(`/urls/${shortURL}`);
});
//*** POST /urls/:id ***
app.post("/urls/:shortURL", (req, res) => {
const user = req.session.user_id;
const shortURL = req.params.shortURL;
if (!user) {
const errorMsg = { message: 'Please login first!!', code: 400 };
res.status(400);
res.render('error', errorMsg);
return;
}
if (users[user].id !== urlDatabase[shortURL].userID) {
const errorMsg = { message: 'This is not your short URL, you dont have permission to edit!', code: 403 };
res.status(403);
res.render('error', errorMsg);
return;
}
const updateURL = req.body.updateURL;
urlDatabase[shortURL].longURL = updateURL;
res.redirect(`/urls`);
});
//*** POST urls/:id/delete ***
app.post("/urls/:shortURL/delete", (req, res) => {
if (!users[req.session.user_id]) {
const errorMsg = { message: 'Please login or register, You dont have permission to delete this url!!!', code: 403 };
res.status(403);
res.render('error', errorMsg);
return;
}
const shortURL = req.params.shortURL;
if (users[req.session.user_id].id !== urlDatabase[shortURL].userID) {
const errorMsg = { message: 'This is not your short URL, you dont have permission to delete!', code: 403 };
res.status(403);
res.render('error', errorMsg);
return;
}
delete urlDatabase[shortURL];
res.redirect('/urls');
});
//**** GET /login ****
app.get("/login", (req, res) => {
if (req.session.user_id) {
res.redirect('/urls');
}
const user = users[req.session.user_id];
const templateVars = {
urls: urlDatabase,
user,
};
res.render('login', templateVars);
});
//**** GET /register ****
app.get("/register", (req, res) => {
if (req.session.user_id) {
res.redirect('/urls');
}
const user = users[req.session.user_id];
const templateVars = {
urls: urlDatabase,
user
};
res.render('register', templateVars);
});
//*** POST /login ***
app.post("/login", (req, res) => {
const email = req.body.email;
const password = req.body.password;
const userExist = getUserByEmail(email, users);
if (email === "") {
const errorMsg = { message: 'Email field or Password is empty, Try again!!', code: 400 };
res.status(400);
res.render('error', errorMsg);
return;
}
if (!userExist) {
const errorMsg = { message: 'Email not found, Please try again or Register.', code: 403 };
res.status(403);
res.render('error', errorMsg);
return;
}
if (!bcrypt.compareSync(password, userExist.password)) {
const errorMsg = { message: 'Password is wrong!! Please try again.', code: 403 };
res.status(403);
res.render('error', errorMsg);
return;
}
if (bcrypt.compareSync(password, userExist.password)) {
const userID = userExist.id;
req.session.user_id = userID;
res.redirect('/urls');
}
});
//*** POST /register ***
app.post("/register", (req, res) => {
const userID = generateRandomString();
const email = req.body.email;
const password = req.body.password;
const userExist = getUserByEmail(email, users);
const hashedPassword = bcrypt.hashSync(password, 10);
if (!email || !password) {
const errorMsg = { message: 'Email field or Password is empty, Try again!!', code: 400 };
res.status(400);
res.render('error', errorMsg);
return;
}
if (userExist) {
const errorMsg = { message: 'This email address exist, Please try to login!', code: 400 };
res.status(400);
res.render('error', errorMsg);
return;
}
users[userID] = { id: userID, email: email, password: hashedPassword };
req.session.user_id = userID;
res.redirect('/urls');
});
//*** POST /logout ***
app.post("/logout", (req, res) => {
req.session = null;
res.redirect('/urls');
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}!`);
});