const bcrypt = require('bcryptjs')
// 密码加密
User.init({
password: {
type: Sequelize.STRING,
set(val) {
const salt = bcrypt.genSaltSync(10)
const psw = bcrypt.hashSync(val, salt)
this.setDataValue('password', psw)
}
}
},
sequelize,
tableName: 'user')
// 密码验证
static async verifyEmailPassword(email, plainPassword) {
const user = await User.findOne({
where: {
email
}
})
if (!user) {
throw new global.errs.AuthFailed('账号不存在')
}
// user.password === plainPassword
const correct = bcrypt.compareSync(
plainPassword, user.password)
if(!correct){
throw new global.errs.AuthFailed('密码不正确')
}
return user
}
网友评论