-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-user.js
More file actions
91 lines (85 loc) · 2.62 KB
/
create-user.js
File metadata and controls
91 lines (85 loc) · 2.62 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
82
83
84
85
86
87
88
89
90
91
var prompt = require('prompt');
var bcrypt = require('bcrypt');
var db = require("./common.js").db;
var config = require("./common.js").config;
// create user table
db.schema.createTableIfNotExists(config.tables.users, function(table) {
table.increments('id');
table.string('username');
table.string('password');
table.string('email');
}).catch(console.log);
// input data schema
var schema = {
properties: {
username: {
description: 'Enter your username',
pattern: /^([a-z0-9]{4,30})$/,
message: 'Username should be only lowercase letters or numbers, length from 4 to 30 characters',
required: true
},
email: {
description: 'Enter your email',
pattern: /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i,
message: 'Please enter correct email',
required: true
},
password: {
description: 'Enter your password',
type: 'string',
pattern: /^([a-zA-Z0-9]{6,128})$/,
message: 'Please enter a password or passphrase, only letters or numbers, length from 6 to 128 characters',
hidden: true,
replace: '*',
required: true
},
password_retype: {
description: 'Retype your password',
type: 'string',
pattern: /^([a-zA-Z0-9]{6,128})$/,
message: 'Please retype the same password',
hidden: true,
replace: '*',
required: true,
conform: function(password_retype) {
var password = prompt.history('password').value;
return (password === password_retype);
}
},
}
};
prompt.start();
// get the properties for the new user
prompt.get(schema, function (err, result) {
// check if the username exists
db.select()
.where('username', result.username)
.from(config.tables.users)
.limit(1)
.then(function(results) {
if (results.length) {
console.log("Error: The username '" + result.username + "' already exists.");
process.exit(1);
}
}).catch(console.log);
// bcrypt the password
var rounds = 1;
var salt = bcrypt.genSaltSync(10);
bcrypt.genSalt(rounds, function(err, salt) {
bcrypt.hash(result.password, salt, function(err, hash) {
// store new user in db
db(config.tables.users).insert({
username: result.username,
email: result.email,
password: hash
}).then(function() {
// log new user data
console.log('New user created:');
console.log(' username: ' + result.username);
console.log(' email: ' + result.email);
console.log(' password hash: ' + hash);
process.exit(0);
}).catch(console.log);
});
});
});