Files

50 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2020-06-27 02:10:10 +02:00
2019-07-07 23:00:31 +02:00
const bcrypt = require('bcryptjs')
2019-07-29 14:10:18 +02:00
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('user', {
settings: {
type: DataTypes.JSON,
defaultValue: []
2020-06-27 02:10:10 +02:00
},
email: {
type: DataTypes.STRING,
unique: { msg: 'error.email_taken' },
validate: {
notEmpty: true
},
index: true,
allowNull: false
},
description: DataTypes.TEXT,
password: DataTypes.STRING,
recover_code: DataTypes.STRING,
is_admin: DataTypes.BOOLEAN,
is_active: DataTypes.BOOLEAN
}, {
scopes: {
withoutPassword: {
attributes: { exclude: ['password', 'recover_code'] }
},
withRecover: {
attributes: { exclude: ['password'] }
}
2020-06-27 02:10:10 +02:00
}
})
User.prototype.comparePassword = async function (pwd) {
if (!this.password) { return false }
return bcrypt.compare(pwd, this.password)
2019-04-03 00:25:12 +02:00
}
User.beforeSave(async (user, _options) => {
if (user.changed('password')) {
const salt = await bcrypt.genSalt(10)
const hash = await bcrypt.hash(user.password, salt)
user.password = hash
}
})
2019-06-07 17:02:33 +02:00
return User
2019-09-11 19:12:24 +02:00
}