-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
183 lines (172 loc) · 4.78 KB
/
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
const bcrypt = require('bcryptjs');
const prompt = require('async-prompt');
const connectDB = require('./config/db');
const chalk = require('chalk');
const clear = require('clear');
const Record = require('./models/Record');
const figlet = require('figlet');
const wait = require('waait');
const month = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
let passwordTries = 0;
//create a new date object
const date = new Date();
//create a default title text
const postDefaultTitle = `${
month[date.getMonth()]
} ${date.getDate()}, ${date.getFullYear()}`;
//the main function
const main = async () => {
clear();
console.log(
figlet.textSync('Open-Journal CLI', {
horizontalLayout: 'default',
verticalLayout: 'default',
whitespaceBreak: true
})
);
console.log('\n\n');
console.log(
chalk.blue(
'Welcome to your personal journal companion interface. You can just make entries using this interface. You cannot edit or view your records.'
)
);
console.log('\n');
//promt a password from the user
passwordChecking('Enter your password: ');
};
//function to prompt password from the user
const passwordPrompting = async (msg) => {
console.log('\n');
const password = await prompt.password(chalk.cyan(msg));
//increase the number of tries that a user does for password entry
passwordTries += 1;
return password;
};
const passwordChecking = async (msg) => {
const password = await passwordPrompting(msg);
//enter the hash of your password here
const passwordHash =
'$2a$10$LFr1Hm6mcBAJHQp5etJ1be11XiVojdKYnKqYzHX.Dn36qgC8P5N02';
//check if the password was correctly entered or not
bcrypt.compare(password, passwordHash, async (err, res) => {
if (res) {
console.log('\n');
console.log(
chalk.bold.bold.rgb(
100,
255,
100
)('Correct password was entered, connecting to the database now.')
);
// if the password was correct, connect to the mongoDB database
await connectDB();
await fillEntry();
}
else {
console.log(
chalk.bold.bold.rgb(255, 100, 100)('Incorrect password was entered.')
);
//cannot try more than 3 passwords at a time
if (passwordTries < 3) {
passwordChecking('Re-enter your password: ');
}
else {
console.log(chalk.red("\nToo many attempts. Exiting now."))
await wait(3000)
clear()
process.exit(1)
}
}
});
};
const fillEntry = async () => {
clear();
console.log(
figlet.textSync('New Record', {
horizontalLayout: 'default',
verticalLayout: 'default',
whitespaceBreak: true
})
);
await wait(3000);
console.log('\n');
//get the inputs from the user
const body = await prompt(chalk.white('Body: '));
console.log('\n');
let title = await prompt(chalk.white(`Title: (${postDefaultTitle}) `));
console.log('\n');
let image = await prompt(chalk.white('Image URL: '));
console.log('\n');
//the the values were not entered, fallback to the default values
if (!title) title = postDefaultTitle;
if (!image) image = null;
//create a mongoose object from the entered values
const newRecord = new Record({
title: title,
imgURL: image,
body: body
});
console.log(chalk.yellow('Saving your responses'));
await wait(3000);
clear();
console.log(
figlet.textSync('New Record', {
horizontalLayout: 'default',
verticalLayout: 'default',
whitespaceBreak: true
})
);
//display the entered values to the user
console.log(chalk.yellow('This is your new post: \n'));
console.log(newRecord);
console.log('\n\n');
//ask if the user is sure to submit the post
const ok = await prompt.confirm(chalk.yellow('Post this entry? '));
if (ok) {
try {
//saving the entry to the mongoDB database
await newRecord.save();
await wait(1500)
clear()
console.log(chalk.green('Your post was added successfully'));
console.log(chalk.red('Exiting now'));
await wait(2000);
clear()
process.exit(0)
} catch (error) {
console.log(chalk.red('There was an error uploading that post'));
//ask if the user wants to try again upon failed save
const tryAgain = await prompt.confirm(chalk.yellow('Do you want to try again? '));
if (tryAgain) {
fillEntry();
} else {
console.log(chalk.red('\nYour post was not saved, exiting now.\n'));
await wait(2000)
clear()
process.exit(1);
}
}
} else {
const ok = await prompt.confirm(chalk.yellow('Do you want to try again? '));
if (ok) {
fillEntry();
} else {
console.log(chalk.red('\nYour post was not saved, exiting now.\n'));
process.exit(1);
}
}
};
main();