62 lines
1.6 KiB
JavaScript
62 lines
1.6 KiB
JavaScript
import path from 'path'
|
|
import config from 'config'
|
|
import mongoose from 'mongoose'
|
|
import { DB } from '../database'
|
|
import credential from 'credential'
|
|
import readlineSync from 'readline-sync'
|
|
import crypto from 'crypto'
|
|
import urlSafeBase64 from 'urlsafe-base64'
|
|
import util from 'util'
|
|
import autobind from 'autobind-decorator'
|
|
|
|
@autobind
|
|
class AddUserTool {
|
|
constructor(toolName, log) {
|
|
this.toolName = toolName
|
|
this.log = log
|
|
}
|
|
|
|
async run() {
|
|
try {
|
|
const mongoUri = config.get('uri.mongo')
|
|
const db = await new DB().connect(mongoUri)
|
|
|
|
console.log(`Connected to MongoDB at '${mongoUri}'`)
|
|
|
|
const User = db.User
|
|
let user = new User({
|
|
administrator: true,
|
|
})
|
|
user.firstName = readlineSync.question('First name? ')
|
|
user.lastName = readlineSync.question('Last name? ')
|
|
user.email = readlineSync.question('Email? ')
|
|
let password = readlineSync.question('Password? ', {hideEchoBack: true})
|
|
let cr = credential()
|
|
|
|
const json = await util.promisify(cr.hash)(password)
|
|
|
|
user.passwordHash = JSON.parse(json)
|
|
|
|
const savedUser = await user.save()
|
|
|
|
console.log(`User is ${user}`)
|
|
} catch(error) {
|
|
console.log(`error: ${error.message}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
const log = {
|
|
info: console.info,
|
|
error: function() { console.error(chalk.red('error:', [...arguments].join(' ')))},
|
|
warning: function() { console.error(chalk.yellow('warning:', [...arguments].join(' ')))}
|
|
}
|
|
|
|
const tool = new AddUserTool('addUser', log)
|
|
|
|
tool.run(process.argv.slice(2)).then((exitCode) => {
|
|
process.exit(exitCode)
|
|
}).catch((err) => {
|
|
console.error(err)
|
|
})
|