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

Commit

Permalink
Merge pull request #893 from lirantal/feature/seeddb-refactor-users
Browse files Browse the repository at this point in the history
Refactoring the seeded user objects to be easily maintained
  • Loading branch information
lirantal committed Sep 9, 2015
2 parents d1a9119 + 05f9053 commit fba6eb7
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: '[email protected]',
firstName: 'User',
lastName: 'Local',
displayName: 'User Local',
roles: ['user']
};

var seedAdmin = {
username: 'admin',
password: '1234',
provider: 'local',
email: '[email protected]',
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: '[email protected]',
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: '[email protected]',
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: '[email protected]',
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 fba6eb7

Please sign in to comment.