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