-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28fe4e9
commit 36dc997
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |