-
Notifications
You must be signed in to change notification settings - Fork 0
/
express_server.js
194 lines (162 loc) · 5.11 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
const {getUserByEmail, generatRandomString, userURLs, currentUser} = require("./helpers/helpers");
const {urlDatabase, users} = require("./helpers/database");
const express = require("express");
const app = express();
const PORT = 8080; // default port 8080
const bodyParser = require("body-parser");
const bcrypt = require("bcryptjs"); //bcrypt added
const cookieSession = require("cookie-session");
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
cookieSession({
name: "session",
keys: ["1233434344"],
})
);
app.get("/", (req, res) => {
if (req.session.userID) {
res.render(`/urls`);
} else {
res.redirect('/login');
}
});
//url index page
app.get("/urls", (req, res) => {
if (currentUser(req.session.userID, users)) {
const userID = req.session.userID;
const userUrls = userURLs(userID, urlDatabase);
const templateVars = {
urls: userUrls,
user: users[userID]
};
res.render('urls_index', templateVars);
} else {
res.redirect("login");
}
});
// urls with user id
app.post("/urls", (req, res) => {
if (currentUser(req.session.userID, users)) {
const shortURL = generatRandomString();
urlDatabase[shortURL] = {
userID: req.session.userID,
longURL: req.body.longURL
};
res.redirect(`/urls/${shortURL}`);
} else {
res.status(401).send('Please log in to proceed');
}
});
// create new short url with login check
app.get("/urls/new", (req, res) => {
if (req.session.userID) {
let templateVars = { user: users[req.session.userID] };
return res.render("urls_new", templateVars);
}
return res.redirect("/login");
});
//user specific urls showed
app.get("/urls/:shortURL", (req, res) => {
if (urlDatabase[req.params.shortURL]) {
let templateVars = {
shortURL: req.params.shortURL,
longURL: urlDatabase[req.params.shortURL].longURL,
urlUser: urlDatabase[req.params.shortURL].userID,
user: users[req.session.user_id],
};
res.render('urls_show', templateVars);
} else {
res.send("<h4>This url is not belong you</h4>")
}
});
app.get("/u/:shortURL", (req, res) => {
if (urlDatabase[req.params.shortURL]) {
const longURL = urlDatabase[req.params.shortURL].longURL;
res.redirect(longURL);
} else {
res.status(404).send('This short URL does not exist');
}
});
//register page route
app.get("/register", (req, res) => {
let varTemplate = { user: users[req.session.userID] };
res.render("register", varTemplate);
});
//registering user email and password
app.post("/register", (req, res) => {
if (req.body.email && req.body.password) {
if (!getUserByEmail(req.body.email, users)) {
const userID = generatRandomString();
users[userID] = {
userID,
email: req.body.email,
password: bcrypt.hashSync(req.body.password, 10)
};
req.session.userID = userID;
res.redirect('/urls');
} else {
res.statusCode = 400;
return res.send("<h4>Please Login, email registered</h4>");
}
} else {
res.statusCode = 400;
return res.send("<h4>Email or password can not be empty!</h4>");
}
});
// only can delete own urls
app.post("/urls/:shortURL/delete", (req, res) => {
const shortURL = req.params.shortURL;
if (req.session.userID === urlDatabase[req.params.shortURL].userID) {
delete urlDatabase[req.params.shortURL];
}
return res.redirect("/urls");
});
//only can edit own urls
app.post("/urls/:shortURL/edit", (req, res) => {
res.redirect(`/urls/${req.params.shortURL}`);
});
//user can edit own links, else asked to login
app.post("/urls/:shortURL/update", (req, res) => {
if (!req.session['userID']) {
res.send('<h4>User should login</h4><a href="http://localhost:8080/login">Login HERE!</a>');
} else if (urlDatabase[req.params.shortURL].userID === req.session['userID']) {
urlDatabase[req.params.shortURL].longURL = req.body.longURL;
res.redirect('/urls');
} else {
res.send('<h4>URL does not exist</h4>');
}
});
// login page
app.get("/login", (req, res) => {
const templateVars = {
email: users[req.session.email],
user: users[req.session.userID]
};
res.render('login', templateVars);
});
//adding login functionality
app.post("/login", (req, res) => {
if (!req.body.email || !req.body.password) {
res.status(403);
return res.send("<h4>Email or Password field can not be empty</h4><a href='http://localhost:8080/login'>Login HERE!</a>");
}
const user = getUserByEmail(req.body.email, users);
if (user === undefined) {
res.status(403);
return res.send("<h4>User is not registered!</h4><a href='http://localhost:8080/register'>Register HERE!</a>");
} else if (bcrypt.compareSync(req.body.password, user.password)) {
req.session.userID = user.userID;
return res.redirect('/urls');
} else {
return res.send("<h4>Email and Password is not matching!</h4><a href='http://localhost:8080/login'>Login HERE!</a>");
}
});
//the Logout route
app.post("/logout", (req, res) => {
req.session = null;
res.redirect("/login");
});
app.listen(PORT, () => {
console.log(`TinyApp listening on port ${PORT}!`);
});