-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress_server.js
More file actions
268 lines (208 loc) · 8.26 KB
/
express_server.js
File metadata and controls
268 lines (208 loc) · 8.26 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const cookieSession = require('cookie-session');
const bcrypt = require('bcryptjs');
const { getUserByEmail, generateRandomString, urlsForUser } = require('./helpers');
const express = require('express');
const app = express();
const PORT = 8080; // default port 8080
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
app.use(cookieSession({
name: 'session',
keys: ['key1', 'key2']
}));
const urlDatabase = {
'b2xVn2': {
longURL: 'http://www.lighthouselabs.ca',
userID: 'aJ48lW'
},
'9sm5xK': {
longURL: 'http://www.google.com',
userID: 'aJ48lW'
}
};
const users = {
aJ48lW: {
id: "userRandomID",
email: "user@example.com",
//unhashed-password: purple-monkey-dinosaur
password: "$2a$10$JuABe1Q3OFNQqYlxgEzLfe7Z8CAvWaRqepr7TF6wyqMAbPcxHzuR6",
},
user2RandomID: {
id: "user2RandomID",
email: "user2@example.com",
//unhashed-password: dishwasher-funk
password: "$2a$10$r44rANeJwt/PURCaYHDOhuBGh662XtGTwKyhYHQgfIZ6yjdUDyVXS",
},
};
app.get('/', (req, res) => {
const userID = users[req.session.user_id];
const templateVars = {user: userID, prompt: '' };
//if user is logged in, redirect to /urls page
if (req.session.user_id) return res.redirect('/urls');
res.render('urls_login', templateVars);
});
app.get('/urls', (req, res) => {
const urls = urlsForUser(req.session.user_id, urlDatabase);
const userID = users[req.session.user_id];
const templateVars = { urls: urls, user: userID, prompt: '' };
//if user is not logged in, redirect to login page
if (!req.session.user_id) {
templateVars.prompt = 'Please login to use this service!';
return res.render('urls_login', templateVars);
}
res.render('urls_index', templateVars);
});
app.post('/urls', (req, res) => {
//if user is not logged in, respond with status code and error message
if (!req.session.user_id) return res.status(403).send('Please login to use this service!');
const randomKey = generateRandomString();
//append http:// to URL if it was not included in the input
if (!req.body.longURL.includes('http://')) {
urlDatabase[randomKey] = {
longURL: `http://${req.body.longURL}`,
userID: req.session.user_id
};
} else {
urlDatabase[randomKey] = {
longURL: req.body.longURL,
userID: req.session.user_id
};
}
res.redirect(`/urls/${randomKey}`);
});
app.get('/urls/new', (req, res) => {
const userID = users[req.session.user_id];
const templateVars = { user: userID, prompt: '' };
//if user is not logged in, redirect to login page
if (!req.session.user_id) {
templateVars.prompt = 'Please login to use this service!';
return res.render('urls_login', templateVars);
}
res.render('urls_new', templateVars);
});
app.get('/u/:id', (req, res) => {
const shortURL = req.params.id;
//if url id is not in database, respond with status code and error message
if (!urlDatabase[shortURL]) return res.status(404).render('urls_404');
const longURL = urlDatabase[shortURL]['longURL'];
res.redirect(longURL);
});
app.get('/urls/:id', (req, res) => {
const shortURL = req.params.id;
//if url id is not in database, respond with status code and error message
if (!urlDatabase[shortURL]) return res.status(404).render('urls_404');
const longURL = urlDatabase[shortURL]['longURL'];
const userID = users[req.session.user_id];
const templateVars = { id: shortURL, longURL: longURL, user: userID, prompt: '' };
//if user is not logged in, redirect to login page
if (!req.session.user_id) {
templateVars.prompt = 'Please login to modify this URL!';
return res.render('urls_login', templateVars);
}
//if userid does not match URL's, redirect to login page
if (req.session.user_id !== urlDatabase[shortURL]['userID']) return res.status(403).render('urls_403');
res.render('urls_show', templateVars);
});
app.post('/urls/:id/delete', (req, res) => {
const shortURL = req.params.id;
//if url id is not in database, respond with status code and error message
if (!urlDatabase[shortURL]) return res.status(404).send('URL not found!');
// //if a user is not logged in, respond with status code and error message
if (!req.session.user_id) return res.status(401).send('Please login to modify this URL!');
//if user do not own the URL, respond with status code and error message
if (req.session.user_id !== urlDatabase[shortURL]['userID']) return res.status(403).send('You don\'t have permission to modify this URL!');
delete urlDatabase[shortURL];
res.redirect(`/urls`);
});
app.post('/urls/:id', (req, res) => {
const shortURL = req.params.id;
//if url id is not in database, respond with status code and error message
if (!urlDatabase[shortURL]) return res.status(404).send('URL not found!');
// //if user is not logged in, respond with status code and error message
if (!req.session.user_id) return res.status(401).send('Please login to modify this URL!');
//if user do not own the URL, respond with status code and error message
if (req.session.user_id !== urlDatabase[shortURL]['userID']) return res.status(403).send('You don\'t have permission to modify this URL!');
//append http:// to URL if it was not included in the input
if (!req.body.longURL.includes('http://')) {
urlDatabase[shortURL] = {
longURL: `http://${req.body.longURL}`,
userID: req.session.user_id
};
} else {
//add new URL to database
urlDatabase[shortURL] = {
longURL: req.body.longURL,
userID: req.session.user_id
};
}
res.redirect(`/urls`);
});
app.get('/register', (req, res) => {
//if user is already logged in, redirect to urls page
if (req.session.user_id) return res.redirect('/urls');
const userID = users[req.session.user_id];
const templateVars = { user: userID, prompt: '' };
res.render('urls_register', templateVars);
});
app.post('/register', (req, res) => {
const userID = users[req.session.user_id];
const templateVars = { user: userID, prompt: '' };
//return 400 status code if email and/or password is empty
if (req.body.email === '' || req.body.password === '') {
templateVars.prompt = 'Email and/or password cannot be blank!';
return res.status(400).render('urls_register', templateVars);
}
const uID = getUserByEmail(req.body.email, users);
//return 400 status code if email already exist
if (uID && users[uID]['email'] === req.body.email) {
templateVars.prompt = 'Email already exist! Please login instead.';
return res.status(400).render('urls_register', templateVars);
}
const randomKey = generateRandomString();
//add new user information to users object if there is no error
users[randomKey] = {
id: randomKey,
email: req.body.email,
//hash password using bcrypt
password: bcrypt.hashSync(req.body.password, 10)
};
req.session.user_id = randomKey;
res.redirect(`/urls`);
});
app.get('/login', (req, res) => {
//if user is already logged in, redirect to urls page
if (req.session.user_id) return res.redirect('/urls');
const userID = users[req.session.user_id];
const templateVars = { user: userID, prompt: '' };
res.render('urls_login', templateVars);
});
app.post('/login', (req, res) => {
const userID = users[req.session.user_id];
const templateVars = { urls: urlDatabase, user: userID, prompt: '' };
//return 400 status code if email and/or password is empty
if (req.body.email === '' || req.body.password === '') {
templateVars.prompt = 'Email and/or password cannot be blank!';
return res.status(400).render('urls_login', templateVars);
}
const uID = getUserByEmail(req.body.email, users);
//return 403 status code if email does not exist
if (!uID) {
templateVars.prompt = 'Email not found! Please register to continue.';
return res.status(403).render('urls_login', templateVars);
}
//use bcrypt to check password match
if (!bcrypt.compareSync(req.body.password, users[uID]['password'])) {
templateVars.prompt = 'Incorrect login! Please try again.';
//return 403 status code if password is incorrect
return res.status(403).render('urls_login', templateVars);
}
req.session.user_id = uID;
res.redirect(`/urls`);
});
app.post('/logout', (req, res) => {
req.session = null;
res.redirect(`/login`);
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}!`);
});