-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackend.js
More file actions
85 lines (74 loc) · 1.86 KB
/
backend.js
File metadata and controls
85 lines (74 loc) · 1.86 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
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
const bcrypt = require("bcrypt");
const inquirer = require("inquirer");
const dotenv = require('dotenv');
// Capture the variables parsed from the .env file
const result = dotenv.config();
if (result.error) {
throw result.error;
}
const wenv = result.parsed;
const fs = require('fs');
const os = require('os');
//fs.writeFileSync('./.env', ENV_VARS.join(os.EOL));
//process.loadEnvFile();
const users = JSON.parse(wenv.USERS_JSON);
async function encrypt(password) {
const hash = await bcrypt.hash(password, 10);
return hash;
};
async function askCredentials() {
const questions = [
{
type: "input",
name: "username",
message: "Enter your username:",
validate: (input) => {
if (input.length < 3) {
return 'Username must be at least 3 characters long.';
}
if (users[input]) {
return 'Username is already registered.';
}
return true;
},
},
{
type: "password",
name: "password",
message: "Enter your password:",
mask: '*',
validate: (input) => {
if (input.length < 8) {
return 'Password should be at least 8 characters';
}
return true;
},
},
{
type: "password",
name: "cpassword",
message: "Confirm your password:",
mask: '*',
validate: (input, answers) => {
if (input !== answers.password) {
return 'Passwords do not match. Please try again.';
}
return true;
},
},
];
inquirer.prompt(questions).then(async (answers) => {
users[answers.username] = await encrypt(answers.password);
console.log("a", await encrypt(answers.password));
wenv.USERS_JSON = JSON.stringify(users);
let ENV_VARS = [];
Object.keys(wenv).forEach((k) => {
console.log(`${k}=${wenv[k]}`);
ENV_VARS.push(`${k}=${wenv[k]}`);
});
/* Add an empty line */
ENV_VARS.push('');
fs.writeFileSync('./.env', ENV_VARS.join(os.EOL));
});
}
askCredentials();