1
0
ohmyform/scripts/create_admin.js

48 lines
1.0 KiB
JavaScript
Raw Normal View History

2017-04-03 13:28:24 -07:00
var config = require('../config/config'),
2017-03-30 10:36:30 -07:00
mongoose = require('mongoose'),
chalk = require('chalk');
2017-03-30 10:36:30 -07:00
2017-04-03 13:28:24 -07:00
exports.run = function(app, db, cb) {
2017-11-21 15:29:47 -08:00
console.log(chalk.green('Creating the Admin Account'));
2017-04-03 13:28:24 -07:00
var User = mongoose.model('User');
2019-07-01 07:13:21 -07:00
var email = config.admin.email || 'admin@admin.com';
2017-11-21 15:29:47 -08:00
var newUserObj = {
2017-04-03 13:28:24 -07:00
firstName: 'Admin',
lastName: 'Account',
2017-10-18 17:30:51 -07:00
email: email,
2019-07-01 07:13:21 -07:00
username: config.admin.username || 'root',
password: config.admin.password || 'root',
2017-04-03 13:28:24 -07:00
provider: 'local',
roles: ['admin', 'user']
};
2017-04-03 13:28:24 -07:00
2017-11-21 15:29:47 -08:00
var options = {
upsert: true,
new: true,
2017-11-21 15:29:47 -08:00
setDefaultsOnInsert: true
};
2017-04-03 13:28:24 -07:00
User.findOneAndUpdate({username: newUserObj.username}, newUserObj, options, function (err, currUser1) {
2017-04-03 13:28:24 -07:00
if (err) {
2017-11-21 15:29:47 -08:00
return cb(err);
2017-04-03 13:28:24 -07:00
}
2017-11-21 15:29:47 -08:00
if(!currUser1){
return cb(new Error('Couldn\'t create admin account'));
2017-11-21 15:29:47 -08:00
} else {
currUser1.password = config.admin.password;
currUser1.save(function(err, currUser2){
if (err) {
return cb(err);
2017-04-03 13:28:24 -07:00
}
2017-11-21 15:29:47 -08:00
console.log(chalk.green('Successfully created/updated Admin Account'));
return cb();
2017-04-03 13:28:24 -07:00
});
}
2017-04-20 22:04:07 -07:00
});
};