forked from tdjsnelling/sqtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateAdminUser.js
More file actions
35 lines (33 loc) · 846 Bytes
/
Copy pathcreateAdminUser.js
File metadata and controls
35 lines (33 loc) · 846 Bytes
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
import bcrypt from 'bcrypt'
import crypto from 'crypto'
import jwt from 'jsonwebtoken'
import User from '../schema/user'
const createAdminUser = async () => {
const existingAdmin = await User.findOne({ username: 'admin' }).lean()
if (!existingAdmin) {
const created = Date.now()
const hash = await bcrypt.hash('admin', 10)
const adminUser = new User({
username: 'admin',
email: 'admin@sqtracker',
role: 'admin',
password: hash,
created,
})
adminUser.uid = crypto
.createHash('sha256')
.update(adminUser._id.toString())
.digest('hex')
.slice(0, 10)
adminUser.token = jwt.sign(
{
id: adminUser._id,
created,
role: 'admin',
},
process.env.SQ_JWT_SECRET
)
await adminUser.save()
}
}
export default createAdminUser