-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathusers.js
45 lines (43 loc) · 938 Bytes
/
users.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
import bcrypt from 'bcrypt';
export default (sequelize, DataType) => {
const Users = sequelize.define('Users', {
id: {
type: DataType.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataType.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
email: {
type: DataType.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
password: {
type: DataType.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
},
{
hooks: {
beforeCreate: user => {
const salt = bcrypt.genSaltSync();
user.set('password', bcrypt.hashSync(user.password, salt));
},
},
classMethods: {
isPassword: (encodedPassword, password) => bcrypt.compareSync(password, encodedPassword),
},
});
return Users;
};