-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.js
38 lines (33 loc) · 766 Bytes
/
User.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Sequelize, { Model } from 'sequelize';
import bcrypt from 'bcrypt';
class User extends Model {
static init(sequelize) {
super.init(
{
name: Sequelize.STRING,
email: Sequelize.STRING,
password: Sequelize.VIRTUAL,
password_hash: Sequelize.STRING,
},
{
sequelize,
}
);
this.addHook('beforeSave', async user => {
if (user.password) {
user.password_hash = await bcrypt.hash(user.password, 8);
}
return this;
});
}
static associate(models) {
this.hasOne(models.Avatar, {
foreignKey: 'user_id',
as: 'avatar',
});
}
checkPassword(password) {
return bcrypt.compare(password, this.password_hash);
}
}
export default User;