Skip to content

Commit

Permalink
Add user command
Browse files Browse the repository at this point in the history
  • Loading branch information
kucingbasah737 committed Nov 26, 2023
1 parent 28fe4e9 commit 36dc997
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const insertTarget = require('./lib/cmd/insert-target');
const insertHostname = require('./lib/cmd/insert-hostname');
const hostnameList = require('./lib/cmd/hostname-list');
const targetList = require('./lib/cmd/target-list');
const addUser = require('./lib/cmd/add-user');

const runWebServer = async () => {
// eslint-disable-next-line global-require
Expand All @@ -37,6 +38,7 @@ const runWebServer = async () => {

// eslint-disable-next-line no-unused-vars
const { argv } = yargs(hideBin(process.argv))
.command('add-user', 'add a new user', () => {}, addUser)
.command('update-password', 'update user password', () => {}, updatePassword)
.command('insert-target', 'insert a new target', () => {}, insertTarget)
.command('insert-hostname', 'insert a new hostname', () => {}, insertHostname)
Expand Down
36 changes: 36 additions & 0 deletions lib/add-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const MODULE_NAME = 'ADD-USER';

const TABLE_NAME = 'users';
const SALT_ROUND = 10;

const bcrypt = require('bcrypt');
const logger = require('./logger');
const mysql = require('./mysql');

module.exports = async (xid, email, password, isSuper) => {
const query = 'INSERT INTO ?? SET ?';
const values = [
TABLE_NAME,
{
email,
password: await bcrypt.hash(password, SALT_ROUND),
super: isSuper ? 1 : 0,
},
];

try {
const [result] = await mysql.poolPromise.query(query, values);
return result;
} catch (e) {
const newE = new Error(`${MODULE_NAME} 8E028548: Exception`);

logger.warn(newE.message, {
xid,
eCode: e.code,
eMessage: e.message || e.toString(),
query: mysql.formatSimplified(query, values),
});

throw newE;
}
};
47 changes: 47 additions & 0 deletions lib/cmd/add-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable no-console */
const prompt = require('prompt');

module.exports = async () => {
// eslint-disable-next-line global-require
const addUser = require('../add-user');

prompt.start();
const promptResult = await prompt.get([
{
name: 'email',
message: 'User email',
required: true,
},
{
name: 'isSuper',
message: 'Is super user (true/[false])',
required: false,
type: 'boolean',
},
{
name: 'newPassword',
message: 'New password',
required: true,
hidden: true,
},
{
name: 'newPassword1',
message: 'Retype new password',
required: true,
hidden: true,
},
]);

if (promptResult.newPassword !== promptResult.newPassword1) {
console.warn('Password does not match');
process.exit(0);
}

try {
await addUser(null, promptResult.email, promptResult.newPassword, promptResult.isSuper);
console.log(`Password for ${promptResult.email} updated`);
} catch (e) {
console.warn('Exception');
}
process.exit(0);
};

0 comments on commit 36dc997

Please sign in to comment.