This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #893 from lirantal/feature/seeddb-refactor-users
Refactoring the seeded user objects to be easily maintained
- Loading branch information
Showing
1 changed file
with
29 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|