hardlounge/src/command-line/users/reset.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

"use strict";
2018-06-15 20:31:06 +00:00
const log = require("../../log");
2018-03-02 18:28:54 +00:00
const colors = require("chalk");
const program = require("commander");
const fs = require("fs");
const Helper = require("../../helper");
const Utils = require("../utils");
2014-09-11 21:00:08 +00:00
program
.command("reset <name>")
.description("Reset user password")
.on("--help", Utils.extraHelp)
2014-09-11 21:00:08 +00:00
.action(function(name) {
if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.getUsersPath()} does not exist.`);
return;
}
const ClientManager = require("../../clientManager");
const users = new ClientManager().getUsers();
if (users === undefined) { // There was an error, already logged
return;
}
if (!users.includes(name)) {
log.error(`User ${colors.bold(name)} does not exist.`);
2014-09-11 21:00:08 +00:00
return;
}
2018-01-11 11:33:36 +00:00
const file = Helper.getUserConfigPath(name);
const user = require(file);
log.prompt({
text: "Enter new password:",
silent: true,
2014-09-11 21:00:08 +00:00
}, function(err, password) {
if (err) {
return;
}
2016-10-21 19:00:43 +00:00
user.password = Helper.password.hash(password);
user.sessions = {};
2014-09-11 21:00:08 +00:00
fs.writeFileSync(
file,
2016-06-19 17:16:50 +00:00
JSON.stringify(user, null, "\t")
2014-09-11 21:00:08 +00:00
);
log.info(`Successfully reset password for ${colors.bold(name)}.`);
2014-09-11 21:00:08 +00:00
});
});