forked from tyleryasaka/TrackCOVID-community
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-admin-user.js
46 lines (42 loc) · 1.35 KB
/
create-admin-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()
const readline = require('readline')
const Writable = require('stream').Writable
var mongoose = require('mongoose')
const User = require('./models/user')
const mutableStdout = new Writable({
write: function (chunk, encoding, callback) {
if (!this.muted) {
process.stdout.write(chunk, encoding)
}
callback()
}
})
const rl = readline.createInterface({
input: process.stdin,
output: mutableStdout,
terminal: true
})
mutableStdout.muted = false
rl.question('\nEnter your production mongodb url (e.g. mongodb://user:[email protected]:27017)\n', function (mongodbUri) {
mongoose.connect(mongodbUri, { useNewUrlParser: true })
const db = mongoose.connection
db.once('open', function () {
rl.question('\nEnter your new admin username\n', function (newUsername) {
rl.question('\nEnter your new admin password\n', function (newPass) {
mutableStdout.muted = false
const newUser = new User({
username: newUsername,
canUploadCheckpoints: true,
canCreateCheckpoints: true,
canManageUsers: true,
canAccessReports: true
})
User.register(newUser, newPass, function () {
rl.write(`\nRegistered user ${newUsername} successfully\n`)
process.exit()
})
})
mutableStdout.muted = true
})
})
})