-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
103 lines (93 loc) · 2.56 KB
/
helpers.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
/////////////////////////////////// HELPERS ///////////////////////////////////////////////
/**
* Function to generate a random string thar will be use as a unique identifier for tinyurls
* @param {number} lengthOfId
* @returns {string}
*/
const generateRandomString = (lengthOfId) => {
let randomString = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let n = 0; n < lengthOfId; n++) {
randomString += characters[Math.floor(Math.random() * characters.length)];
}
return randomString;
};
/**
* Function to check if an user exists into the database.
* @param {string} id
* @param {object} database
* @returns {object}
*/
const getUserByEmail = (email, database) => {
const id = Object.keys(database).find(key => database[key].email === email);
if (id) {
return database[id];
}
return null;
};
/**
* Function to obtain the urls for a particular user
* @param {string} id
* @returns {object}
*/
const urlsForUser = (id, database) => {
const userUrls = {};
for (let url in database) {
if (database[url].userID === id) {
userUrls[url] = database[url];
}
}
return userUrls;
};
/**
* Function to validate if user is valid and logged in
* @param {object} req - The request object.
* @param {object} res - The response object
* @returns {string}
*/
const validateLogin = (req, res, shouldRedirect = false) => {
const userId = req.session.userId;
if (!userId) {
if (shouldRedirect) {
res.redirect("/login");
} else {
res.status(401).render("status401");
}
} else {
return userId;
}
};
/**
* Function that validates ownership of a URL in the database and executes a callback
* if valid.
* @param {object} req - The request object.
* @param {object} res - The response object
* @param {Function} callback - Callback function to execute if url ownership is valid
* @returns {string} - Id of the valid url
*/
const validateUrlOwnership = (req, res, urlDatabase, userDatabase, callback)=> {
const userID = validateLogin(req, res);
if (userID) {
const id = req.params.id;
// Checks if the id exists in the database
const urlObject = urlDatabase[id];
if (urlObject) {
// Validates ownership of the URL
if (urlObject.userID === userID) {
callback(id, userDatabase[userID]);
} else {
res.status(403).render("status403");
}
} else {
res.status(404).render("status404");
}
}
};
module.exports = {
generateRandomString,
getUserByEmail,
urlsForUser,
validateLogin,
validateUrlOwnership
};