Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Refactoring the seeded user objects to be easily maintained
Browse files Browse the repository at this point in the history
  • Loading branch information
lirantal committed Sep 7, 2015
1 parent 522cc6b commit 05f9053
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions config/lib/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,37 @@ var mongoose = require('mongoose'),

console.log(chalk.bold.red('Warning: Database seeding is turned on'));

var seedUser = {
username: 'user',
password: '1234',
provider: 'local',
email: 'user@localhost.com',
firstName: 'User',
lastName: 'Local',
displayName: 'User Local',
roles: ['user']
};

var seedAdmin = {
username: 'admin',
password: '1234',
provider: 'local',
email: 'admin@localhost.com',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
};


//If production only seed admin if it does not exist
if (process.env.NODE_ENV === 'production') {
//Add Local Admin
User.find({username: 'admin'}, function (err, users) {
if (users.length === 0) {
var password = crypto.randomBytes(64).toString('hex').slice(1, 20);
var user = new User({
username: 'admin',
password: password,
provider: 'local',
email: 'admin@localhost.com',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
});
seedAdmin.password = password;
var user = new User(seedAdmin);
// Then save the user
user.save(function (err) {
if (err) {
Expand All @@ -39,16 +54,8 @@ if (process.env.NODE_ENV === 'production') {
//Add Local User
User.find({username: 'user'}).remove(function () {
var password = crypto.randomBytes(64).toString('hex').slice(1, 20);
var user = new User({
username: 'user',
password: password,
provider: 'local',
email: 'user@localhost.com',
firstName: 'User',
lastName: 'Local',
displayName: 'User Local',
roles: ['user']
});
seedUser.password = password;
var user = new User(seedUser);
// Then save the user
user.save(function (err) {
if (err) {
Expand All @@ -63,16 +70,8 @@ if (process.env.NODE_ENV === 'production') {
//Add Local Admin
User.find({username: 'admin'}).remove(function () {
var password = crypto.randomBytes(64).toString('hex').slice(1, 20);
var user = new User({
username: 'admin',
password: password,
provider: 'local',
email: 'admin@localhost.com',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
});
seedAdmin.password = password;
var user = new User(seedAdmin);
// Then save the user
user.save(function (err) {
if (err) {
Expand Down

0 comments on commit 05f9053

Please sign in to comment.