-
Notifications
You must be signed in to change notification settings - Fork 36
/
add_user.js
71 lines (60 loc) · 1.74 KB
/
add_user.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
var colors = require('colors/safe');
var fs = require('fs');
var readline = require('readline');
var CryptoJS = require("crypto-js");
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
var ticker = 0;
var user = {};
var users;
var usersFile;
if (fs.existsSync('users.json')) {
try {
var usersFile = fs.readFileSync('users.json', 'utf8');
users = JSON.parse(usersFile);
console.info(colors.green('Found users.json file'));
console.info(colors.green('Contains '+ users.length + ' users'));
} catch (e) {
console.error(colors.red('users.json is not a valid json'));
}
} else {
var fd = fs.openSync('users.json', 'w');
console.info(colors.green('users.json file created'));
users = [];
}
console.log(colors.yellow.underline('Create a user:'));
console.log(colors.yellow('Enter the username:'));
// found on http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
function generateUUID(){
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
}
rl.on('line', function(line){
switch (ticker) {
case 0:
user.username = line;
console.log(colors.yellow('Enter the password:'));
break;
case 1:
user.password = CryptoJS.SHA256(line).toString(CryptoJS.enc.Hex);
user.id = generateUUID();
users.push(user);
fs.writeFile("users.json", JSON.stringify(users), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
rl.close();
break;
}
ticker++;
});