You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.3 KiB

3 years ago
import Repository from './repository.js'
import bcrypt from 'bcrypt'
/**
*
*
* @author Björn Hase, me@herr-hase.wtf
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.node001.net/HerrHase/super-hog
*
*/
class UserRepository extends Repository
{
constructor()
{
super()
this.type = 'user'
this.index.push('username')
this.index.push('sessionId')
}
/**
*
* @param {[type]} data
* @return {[type]}
*/
beforeCreate(data)
{
if (data.password) {
const salt = bcrypt.genSaltSync(15)
data.password = bcrypt.hashSync(data.password, salt)
}
return data
}
/**
* getting user by username
*
* @param {string} username
* @return {promise}
*/
async findOneByUsername(username)
{
return this.findOne({
'selector': {
'username': username
}
})
}
/**
* getting user by sessionToken
*
* @param {string} sessionToken
* @return {promise}
*/
findOneBySessionId(sessionId)
{
return this.findOne({
'selector': {
'sessionId': sessionId
}
})
}
}
export default UserRepository