-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.js
More file actions
86 lines (83 loc) · 3.19 KB
/
js.js
File metadata and controls
86 lines (83 loc) · 3.19 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
// app.get(`/urls/:id`, (req, res) => {
// let userId = req.session.email;
// console.log(user[userId])
// if (!userId) {
// res.status(401).send("You must be logged in to view and create a tiny URL!");
// // if user is not logged in:
// } else if (userId !== user[userId].email) {
// res.status(403).send("You do not have the necessary permissions to access this link. Login with this <a href='http://localhost:8080/login'>link</a> to access your own tiny url links.");
// // if logged in user does not match the user that owns this url:
// } else if (!user[userId].urlDatabase[req.params.id]) {
// res.status(404).send("That URL does not exist!");
// // if url w/ :id does not exist:
// } else {
// let templateVars = {
// email: userId,
// shortURL: req.params.id,
// longURL: user[userId].urlDatabase[req.params.id],
// urls: user[userId].urlDatabase };
// res.status(200).render("urls_show", templateVars);
// }
// });
app.get(`/urls/:id`, (req, res) => {
let userId = req.session.email;
console.log(user[userId])
if (userId) {
if (userId in user) {
if (req.params.id in user[userId].urlDatabase) {
let templateVars = {
email: userId,
shortURL: req.params.id,
longURL: user[userId].urlDatabase[req.params.id],
urls: user[userId].urlDatabase };
res.status(200).render("urls_show", templateVars);
} else {
res.status(404).send("That URL does not exist!");
// if url w/ :id does not exist:
}
} else {
res.status(403).send("You do not have the necessary permissions to access this link. Login with this <a href='http://localhost:8080/login'>link</a> to access your own tiny url links.");
// if logged in user does not match the user that owns this url:
}
} else {
res.status(401).send("You must be logged in to view and create a tiny URL!");
// if user is not logged in:
}
});
app.get(`/urls/:id`, (req, res) => {
let userId = req.session.email;
console.log(user[userId]);
if (userId) {
// if the url has an owner, return true
if (findURLByKey()) {
if (req.params.id in user[userId].urlDatabase) {
let templateVars = {
email: userId,
shortURL: req.params.id,
longURL: user[userId].urlDatabase[req.params.id],
urls: user[userId].urlDatabase
};
return res.status(200).render("urls_show", templateVars);
}
return res.status(404).send("That URL does not exist!");
// if url w/ :id does not exist:
} else {
res.status(403).send("You do not have the necessary permissions to access this link. Login with this <a href='http://localhost:8080/login'>link</a> to access your own tiny url links.");
// if logged in user does not match the user that owns this url:
}
} else {
res.status(401).send("You must be logged in to view and create a tiny URL!");
// if user is not logged in:
}
});
function findURLByKey() {
let userId = req.session.email;
for (let key in user) {
for (let users in user[key].urlDatabase) {
if (users === userId) {
return userId;
}
}
}
return -1;
}