50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
|
// noinspection SpellCheckingInspection
|
||
|
|
||
|
import UserModel from '../models/User.js'
|
||
|
// import Bun.password from 'argon2'
|
||
|
import gravatar from 'gravatar'
|
||
|
|
||
|
const createAdmin = async () => {
|
||
|
try {
|
||
|
const hash = await Bun.password.hash('game$databaseadmin1')
|
||
|
|
||
|
const oldUser = await UserModel.findOne({
|
||
|
email: 'admin@gamesdatabase.com',
|
||
|
})
|
||
|
|
||
|
if (oldUser) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
const avatar = gravatar.url('admin@gamesdatabase.com', {
|
||
|
s: '200',
|
||
|
r: 'pg',
|
||
|
d: 'retro',
|
||
|
})
|
||
|
|
||
|
const user = new UserModel({
|
||
|
name: 'Admin',
|
||
|
email: 'admin@gamesdatabase.com',
|
||
|
password: hash,
|
||
|
displayName: 'Administrator',
|
||
|
avatar,
|
||
|
role: 'admin',
|
||
|
gamesCreated: [],
|
||
|
gamesToAccess: [],
|
||
|
})
|
||
|
|
||
|
await user.save((err) => {
|
||
|
console.log('Admin user successfully created')
|
||
|
if (err) {
|
||
|
return console.error('Could not save admin user')
|
||
|
}
|
||
|
})
|
||
|
} catch (err) {
|
||
|
return console.error(
|
||
|
'Server Error: Could not create admin user' + err.message
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default createAdmin
|