-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.js
64 lines (61 loc) · 1.27 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { Model, DataTypes } = require('sequelize')
const sequelize = require('../config/connection')
const bcrypt = require('bcrypt')
class User extends Model {
checkPassword(loginPw) {
return bcrypt.compareSync(loginPw, this.password)
}
}
User.init(
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
full_name: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true,
},
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [6],
},
},
},
{
hooks: {
async beforeCreate(newUserData) {
newUserData.password = await bcrypt.hash(newUserData.password, 10)
return newUserData
},
async beforeUpdate(updatedUserData) {
updatedUserData.password = await bcrypt.hash(
updatedUserData.password,
10
)
return updatedUserData
},
},
sequelize,
timestamps: false,
freezeTableName: true,
underscored: true,
modelName: 'user',
}
)
module.exports = User