-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
42 lines (37 loc) · 1.27 KB
/
Copy pathhelpers.js
File metadata and controls
42 lines (37 loc) · 1.27 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
// Generate Random String of 6 capital/lowercase leters and numbers
const generateRandomString = function() {
return Math.random().toString(36).slice(2, 8);
};
// Check if Session is valid (ie. orphaned session in client on server restart)
const validSessionCheck = function(userId, db) {
return userId in db;
};
// Permission check
const userPermCheck = function(req, res, db) {
if (!(req.params.id in db)) {
return res.status(404).send('Short URL id does not exist!');
} else if (!req.session.userId) {
return res.status(401).send('Must be logged in to access this page');
} else if (db[req.params.id].userId !== req.session.userId) {
return res.status(401).send('Page is only accessible by its owner');
}
};
// Find urls belonging to user from url database
const findUserUrls = function(userId, db) {
const userUrls = {};
for (const shortUrl in db) {
if (userId === db[shortUrl].userId) {
userUrls[shortUrl] = db[shortUrl];
}
}
return userUrls;
};
// Find user object from email value
const findEmailUserObj = function(email, db) {
for (const obj of Object.values(db)) {
if (obj.email === email) {
return (obj);
}
}
};
module.exports = { generateRandomString, userPermCheck, findUserUrls, findEmailUserObj, validSessionCheck };