-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsupport.js
61 lines (57 loc) · 1.9 KB
/
support.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const bcrypt = require('bcrypt');
const crypto = require('crypto');
const MongoClient = require('mongodb').MongoClient;
const objectid = require('mongodb').ObjectID;
const dockerHost = process.env.MONGOHOST || 'localhost';
var mc;
var mdb;
(async function() {
try {
mc = await MongoClient.connect('mongodb://' + dockerHost + '/cop', {
useNewUrlParser: true,
useUnifiedTopology: true
});
mdb = mc.db('cop')
if (await set_user_unique())
await create_user();
process.exit();
} catch (err) {
throw(err);
}
})();
async function set_user_unique() {
try {
await mdb.collection('users').createIndex({ username: 1 }, { unique: true });
return true;
} catch (err) {
if (err.codeName != 'DuplicateKey') {
console.log(err);
return false;
}
}
return true;
}
async function create_user() {
if (process.argv.length === 3) {
var hash;
try {
hash = await bcrypt.hash(process.argv[2], 10);
var api = crypto.randomBytes(32).toString('hex');
var user = { username: 'admin', name: 'admin', password: hash, is_admin: true, api: api, avatar: '', deleted: false };
var row = await mdb.collection('users').findOne({ username: 'admin' });
if (!row) {
var res = await mdb.collection('users').insertOne(user);
var channel = { _id: objectid(res.ops[0]._id), name: '', deleted: false, type: 'user', members: [objectid(res.ops[0]._id)] };
await mdb.collection('channels').insertOne(channel);
} else {
await mdb.collection('users').updateOne({ _id: row._id }, { $set: user });
}
} catch (err) {
console.log(err);
return false;
}
return true;
} else {
return true;
}
}