-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataInitializer.js
48 lines (45 loc) · 1.38 KB
/
dataInitializer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module.exports = {
createUser: function () {
const bcrypt = require('bcryptjs');
const config = require('config');
const jwt = require('jsonwebtoken');
// User Model
const User = require('./models/User');
User.find({ username: process.env.ADMIN_USERNAME }).exec(function (err, docs) {
if (docs.length) {
console.log(process.env.ADMIN_USERNAME + " already exists in db!")
} else {
const newUser = new User({
username: process.env.ADMIN_USERNAME,
password: process.env.ADMIN_PASSWORD
});
// Create salt & hash
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser.save()
.then(user => {
jwt.sign(
{ id: user.id },
config.get('jwtSecret'),
{ expiresIn: 3600 },
(err, token) => {
if (err) throw err;
res.json({
token,
user: {
id: user.id,
name: user.name,
email: user.email
}
});
}
)
});
})
})
}
})
}
}