added createUser to lib

This commit is contained in:
srgooglo 2022-03-14 18:44:26 +01:00
parent a4698663de
commit e956ddb7ec
2 changed files with 33 additions and 3 deletions

View File

@ -0,0 +1,28 @@
import { User } from "../../models"
import Avatars from "dicebar_lib"
import bcrypt from "bcrypt"
export default async function (payload) {
let { username, password, email, fullName, roles, avatar } = payload
const existentUser = await User.findOne({ username: username })
if (existentUser) {
throw new Error("User already exists")
}
const hash = bcrypt.hashSync(password, parseInt(process.env.BCRYPT_ROUNDS ?? 3))
let user = new User({
username: username,
password: hash,
email: email,
fullName: fullName,
avatar: avatar ?? Avatars.generate({ seed: username, type: "initials" }).uri,
roles: roles,
})
user.save()
return user
}

View File

@ -1,3 +1,5 @@
export * as Token from './token'
export { default as Schematized } from './schematized'
export { default as additionsHandler } from './additionsHandler'
export { default as Schematized } from "./schematized"
export { default as additionsHandler } from "./additionsHandler"
export { default as createUser } from "./createUser"
export * as Token from "./token"