diff --git a/cli.js b/cli.js index 608a23f..c679b87 100644 --- a/cli.js +++ b/cli.js @@ -1,7 +1,7 @@ let yargs = require("yargs"); let user = require("./user"); -yargs.scriptName("node cli.js") +yargs.scriptName("node app.js") .command("adduser [username] [password]", "Add user", (yargs) => { yargs.positional("username", { describe: "Username for the new user", @@ -22,6 +22,22 @@ yargs.scriptName("node cli.js") user.setUser(argv.username, argv.password); process.exit(0); }) + .command("setuser [password]", "Change an user password", (yargs) => { + yargs.positional("username", { + describe: "The user to change password", + type: "string" + }); + }, async (argv) => { + let file = user.getFile(); + if (!(argv.username in file)) { + console.error("Invalid username !") + process.exit(1); + } + if (!("password" in argv)) + argv.password = await user.getPassword(); + user.setUser(argv.username, argv.password); + process.exit(0); + }) .command("deluser ", "Remove user", (yargs) => { yargs.positional("username", { type: "string", @@ -30,7 +46,7 @@ yargs.scriptName("node cli.js") }, async (argv) => { process.exit(user.delUser(argv.username)); }) - .command("listuser", "List all users", (argv) => { + .command("listuser", "List all users", () => { let users = []; let file = user.getFile(); for (let u in file) diff --git a/user.js b/user.js index c9128be..c9dd5f4 100644 --- a/user.js +++ b/user.js @@ -36,14 +36,14 @@ async function getPassword() { let rl = getReadLine(); let password = await new Promise(resolve => rl.question("Password: ", resolve)); rl.close(); - return passwordHash.generate(password); + return password; } function setUser(username, password) { let file = getFile(); - file[username] = password; + file[username] = passwordHash.generate(password); fs.writeFileSync("users.json", JSON.stringify(file)); - console.log("User " + username + " created"); + console.log("User " + username + " set"); } async function addUser() {