-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-user.js
46 lines (36 loc) · 1.14 KB
/
create-user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require('dotenv').config()
var assert = require('assert')
var bcrypt = require('bcrypt')
var database = require('./database.js')
const USERNAME = process.argv[2]
const PASSWORD = process.argv[3]
if(!(USERNAME && PASSWORD))
{
console.log('Usage: ' + process.argv[0] + ' ' + process.argv[1] + ' <username> <password>')
process.exit(-1)
}
database.getClient().connect((err, client) => {
assert.equal(null, err)
console.log('Successfully connected to MongoDB')
var db = client.db()
console.log('Creating user "' + USERNAME + '"')
bcrypt.hash(PASSWORD, 10, function(err, hash) {
db.collection('users').findOne({ username: USERNAME }, (err, result) => {
assert.equal(null, err)
if(result)
{
console.log('User with username "' + USERNAME + '" already exists')
process.exit(-1)
}
var params = {
username: USERNAME,
passwordHash: hash
}
db.collection('users').insertOne(params, (err, result) => {
assert.equal(null, err)
console.log('Created user "' + USERNAME + '"')
client.close()
})
})
})
})