-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateAdmin.js
40 lines (36 loc) · 1.01 KB
/
createAdmin.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
const mongoose = require('mongoose');
const Config = require('config');
const Model = require('./src/models/userModel').model;
const email = process.env.npm_config_email;
const config = Config.get('server');
mongoose.connect(config.db, {useNewUrlParser: true}).catch(e => console.log(e));
async function makeUserAdmin(userEmail) {
try {
const user = await Model.findOne({email: userEmail});
if (user) {
if (user.role === 'admin') {
console.log(`User with '${email}' already has admin rights!`);
} else {
user.role = 'admin';
await user.save();
console.log(`Successfully made '${email}' an admin user!`);
}
} else {
console.log(`User with email '${email}' not found!`);
}
} catch(e) {
console.error(e);
}
}
(async () => {
try {
if (email === undefined) {
console.error('Please provide a username with --email=\'email\'');
} else {
await makeUserAdmin(email);
}
mongoose.disconnect().catch(e => console.log(e));
} catch (e) {
console.error(e);
}
})();