hardlounge/src/command-line/add.js

43 lines
1.0 KiB
JavaScript
Raw Normal View History

2014-09-13 21:29:45 +00:00
var ClientManager = new require("../clientManager");
2014-10-03 09:57:35 +00:00
var bcrypt = require("bcrypt-nodejs");
2014-09-13 21:29:45 +00:00
var program = require("commander");
var mkdirp = require("mkdirp");
2014-09-13 21:55:07 +00:00
var Helper = require("../helper");
2014-09-13 21:29:45 +00:00
program
.command("add <name>")
2014-09-13 21:29:45 +00:00
.description("Add a new user")
2015-09-30 22:39:57 +00:00
.action(function(name/* , password */) {
2014-09-13 21:29:45 +00:00
try {
mkdirp.sync(Helper.USERS_PATH);
2014-09-13 21:29:45 +00:00
} catch (e) {
log.error("Could not create", Helper.USERS_PATH);
2014-09-13 21:29:45 +00:00
return;
}
var manager = new ClientManager();
var users = manager.getUsers();
if (users.indexOf(name) !== -1) {
2016-04-16 11:32:38 +00:00
log.error("User '" + name + "' already exists.");
2014-09-13 21:29:45 +00:00
return;
}
require("read")({
prompt: "[thelounge] Enter password: ",
silent: true
}, function(err, password) {
if (!err) {
add(manager, name, password);
}
});
2014-09-13 21:29:45 +00:00
});
2014-09-15 21:31:25 +00:00
2014-09-16 17:33:15 +00:00
function add(manager, name, password) {
2014-10-03 09:57:35 +00:00
var salt = bcrypt.genSaltSync(8);
var hash = bcrypt.hashSync(password, salt);
2014-10-14 20:53:26 +00:00
manager.addUser(
2014-09-16 17:33:15 +00:00
name,
hash
);
2016-04-16 11:32:38 +00:00
log.info("User '" + name + "' created:");
log.info(Helper.getUserConfigPath(name));
2014-09-16 17:33:15 +00:00
}