-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
65 lines (53 loc) · 1.83 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
const bcrypt = require("bcryptjs");
// Helper function to find an email in the users object
const getUserByEmail = (email, database) => {
for (const user in database) {
if (email === database[user].email) {
return user;
}
}
return undefined;
};
const generateRandomString = () => {
// Create a string with all alphanumeric characters and an empty string
const characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let randomString = "";
for (let i = 0; i < 6; i++) {
// Generate a random number between 0 and the number of alphanumeric characters
let randomNumber = Math.floor(Math.random() * characters.length);
randomString += characters[randomNumber];
}
return randomString;
};
// Return an object containing the URLs from the database that belong to the user
const getUserURLs = (id, database) => {
let userURLs = {};
for (const url in database) {
if (id === database[url].userID) {
userURLs[url] = database[url];
}
}
return userURLs;
};
// Helper function for adding new user
const addNewUser = (email, password, database) => {
// Generate a random ID and use it as the user key in the database
let userID = generateRandomString();
// Add the user object into the database and return the ID
database[userID] = {
id: userID,
email,
password: bcrypt.hashSync(password, 10),
};
return userID;
};
// Helper function to authenticate the user trying to log in
const authenticateUser = (email, password, database) => {
const user = getUserByEmail(email, database);
// Check if the entered password matches the password in the database
if (user && bcrypt.compareSync(password, database[user].password)) {
return user;
}
return false;
};
module.exports = { getUserByEmail, generateRandomString, getUserURLs, addNewUser, authenticateUser };