tellform/scripts/create_admin.js

46 lines
971 B
JavaScript
Raw Normal View History

2017-04-03 20:28:24 +00:00
var config = require('../config/config'),
2017-03-30 17:36:30 +00:00
mongoose = require('mongoose'),
2017-04-21 05:04:07 +00:00
chalk = require('chalk');
2017-03-30 17:36:30 +00:00
2017-04-03 20:28:24 +00:00
exports.run = function(app, db, cb) {
2017-11-21 23:29:47 +00:00
console.log(chalk.green('Creating the Admin Account'));
2017-04-03 20:28:24 +00:00
var User = mongoose.model('User');
2017-11-21 23:29:47 +00:00
var username = config.admin.username;
2017-11-21 23:29:47 +00:00
var newUserObj = {
2017-04-03 20:28:24 +00:00
firstName: 'Admin',
lastName: 'Account',
2017-11-21 23:29:47 +00:00
email: config.admin.email,
username: username,
roles: config.admin.roles
};
var options = {
upsert: true,
new: true,
setDefaultsOnInsert: true
}
2017-04-03 20:28:24 +00:00
2017-11-21 23:29:47 +00:00
User.findOneAndUpdate({username: username}, newUserObj, options, function (err, currUser1) {
2017-04-03 20:28:24 +00:00
if (err) {
2017-11-21 23:29:47 +00:00
return cb(err);
2017-04-03 20:28:24 +00:00
}
2017-11-21 23:29:47 +00:00
if(!currUser1){
return cb(new Error('Couldn\'t create admin account'))
} else {
currUser1.password = config.admin.password;
currUser1.save(function(err, currUser2){
if (err) {
return cb(err);
2017-04-03 20:28:24 +00:00
}
2017-11-21 23:29:47 +00:00
console.log(chalk.green('Successfully created/updated Admin Account'));
return cb();
2017-04-03 20:28:24 +00:00
});
}
2017-04-21 05:04:07 +00:00
});
2017-04-03 20:28:24 +00:00
}