-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress_server.js
More file actions
215 lines (177 loc) · 5 KB
/
express_server.js
File metadata and controls
215 lines (177 loc) · 5 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
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
const PORT = 8080;
const bcrypt = require('bcrypt');
const cookieSession = require('cookie-session');
const { getUserByEmail } = require('./helpers');
//user database
const users = {
};
app.use(cookieSession({
name: 'session',
keys: ['beep']
}));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
//dataBase that updates dynamically when users add URLs in their account
let urlDatabase = {
i3BoGr: {
longURL: "https://www.google.ca",
userID: "aJ48lW"
},
b6UTxQ: {
longURL: "https://www.tsn.ca",
userID: "aJ48lW"
}
};
//function finds userID
const getUserById = (id) => {
return users[id];
};
const generateRandomString = (num) => {
let output = '';
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let charLength = characters.length;
for (let i = 0; i < num; i++) {
output += characters.charAt(Math.floor(Math.random() * charLength));
}
return output;
};
//returns a new database of urls that only have the id of the current user who is logged in
const urlsForUser = (id) => {
let result = {};
for (let key in urlDatabase) {
if (urlDatabase[key].userID === id) {
result[key] = urlDatabase[key];
}
}
return result;
};
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}!`);
});
app.get("/urls.json", (req, res) => {
res.json(urlDatabase);
});
app.get("/urls", (req, res) => {
const user = getUserById(req.session.user_id);
let templateVars = {
urls: urlsForUser(req.session.user_id),
user: user
};
res.render("urls_index", templateVars);
});
app.get("/urls/new", (req, res) => {
const user = getUserById(req.session.user_id);
let templateVars = {
urls: urlDatabase,
user: user
};
if (user) {
return res.render("urls_new", templateVars);
} else {
return res.redirect('/login');
}
});
app.get("/urls/:shortURL", (req, res) => {
const user = getUserById(req.session.user_id);
if (urlDatabase[req.params.shortURL].userID !== user.id) {
res.status(400).send('You do not have permissions to edit this url.');
}
const shortURL = req.params.shortURL;
let templateVars = {
shortURL: shortURL,
longURL: urlDatabase[shortURL]['longURL'],
user: user
};
res.render("urls_show", templateVars);
});
app.post("/urls", (req, res) => {
const user = getUserById(req.session.user_id);
let shortURL = generateRandomString(6);
urlDatabase[shortURL] = { longURL: req.body.longURL, userID: user.id };
res.redirect(`/urls/${shortURL}`);
});
app.get("/u/:shortURL", (req, res) => {
const longURL = urlDatabase[req.params.shortURL];
res.redirect(longURL['longURL']);
});
app.post("/urls/:shortURL/delete", (req, res) => {
delete urlDatabase[req.params.shortURL];
res.redirect('/urls');
});
app.post("/urls/:shortURL/edit", (req, res) => {
const user = getUserById(req.session.user_id);
if (urlDatabase[req.params.shortURL].userID !== user.id) {
res.status(400).send('You do not have permissions to edit this url.');
}
const newInfo = {
longURL: req.body.edit,
userID: user.id
};
urlDatabase[req.params.shortURL] = newInfo;
res.redirect('/urls');
});
app.get("/urls:shortURL", (req, res) => {
res.redirect('/url');
});
app.post("/login", (req, res) => {
if (!req.session.user_id) {
const email = req.body.email;
const password = req.body.password;
const userID = getUserByEmail(email, users);
if (userID) {
// Is a valid user based on their e-mail
const user = getUserById(userID);
if (bcrypt.compareSync(password, user.password)) {
// Password matches
req.session.user_id = userID;
return res.redirect('/urls');
}
}
res.status(404).send('Incorrect username or password');
}
});
app.post("/logout", (req, res) => {
req.session = null;
res.redirect('/login');
});
app.get("/register", (req, res) => {
const user = getUserById(req.session.user_id);
let templateVars = {
user: user
};
res.render("urls_registration", templateVars);
});
app.get("/login", (req, res) => {
const user = getUserById(req.session.user_id);
let templateVars = {
user: user
};
res.render("urls_login", templateVars);
});
app.post("/register", (req, res) => {
const email = req.body.email;
const password = req.body.password;
const hashedPassword = bcrypt.hashSync(password, 10);
const id = generateRandomString(3);
const user = {
id: id,
email: email,
password: hashedPassword,
};
//check for existing email before putting new one into the database
if (email === "" || password === "") {
res.status(404).send("You must enter a valid email address and password to create an account");
return;
}
// let dataBase = user.email;
if (getUserByEmail(email, users)) {
res.status(404).send("An account already exists for this email address.");
return;
}
users[id] = user;
req.session.user_id = id;
res.redirect('/urls');
});