This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
first-time-setup.js
110 lines (81 loc) · 2.61 KB
/
first-time-setup.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
const Account = require('./server/models/account');
const Admin = require('./server/models/admin');
const AdminGroup = require('./server/models/admin-group');
const AuthAttempt = require('./server/models/auth-attempt');
const MongoModels = require('mongo-models');
const Promptly = require('promptly');
const Session = require('./server/models/session');
const Status = require('./server/models/status');
const User = require('./server/models/user');
const main = async function () {
let options = {};
// get mongodb connection info
options = {
default: process.env.MONGODB_URI || 'mongodb://localhost:27017/'
};
const mongodbUri = await Promptly.prompt(`MongoDB URI: (${options.default})`, options);
options = {
default: 'frame'
};
const mongodbName = await Promptly.prompt(`MongoDB name: (${options.default})`, options);
// connect to db
const db = await MongoModels.connect({ uri: mongodbUri, db: mongodbName });
if (!db) {
throw Error('Could not connect to MongoDB.');
}
// get root user creds
const rootEmail = await Promptly.prompt('Root user email:');
const rootPassword = await Promptly.password('Root user password:');
// clear tables
await Promise.all([
Account.deleteMany({}),
AdminGroup.deleteMany({}),
Admin.deleteMany({}),
AuthAttempt.deleteMany({}),
Session.deleteMany({}),
Status.deleteMany({}),
User.deleteMany({})
]);
// setup root group
await AdminGroup.create('Root');
// setup root admin and user
await Admin.insertOne(new Admin({
_id: Admin.ObjectId('111111111111111111111111'),
groups: {
root: 'Root'
},
name: {
first: 'Root',
middle: '',
last: 'Admin'
},
user: {
id: '000000000000000000000000',
name: 'root'
}
}));
const passwordHash = await User.generatePasswordHash(rootPassword);
await User.insertOne(new User({
_id: User.ObjectId('000000000000000000000000'),
email: rootEmail.toLowerCase(),
password: passwordHash.hash,
roles: {
admin: {
id: '111111111111111111111111',
name: 'Root Admin'
}
},
username: 'root'
}));
// all done
MongoModels.disconnect();
console.log('First time setup complete.');
process.exit(0);
};
main().catch((err) => {
console.log('First time setup failed.');
console.error(err);
MongoModels.disconnect();
process.exit(1);
});