-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
37 lines (33 loc) · 914 Bytes
/
user.js
File metadata and controls
37 lines (33 loc) · 914 Bytes
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
const bcrypt = require('bcrypt')
const Sequelize = require('sequelize')
const sequelize = require('./database.js')
class User extends Sequelize.Model { }
User.init(
{
email: {
type: Sequelize.DataTypes.STRING,
allowNull: false,
primaryKey: true
},
password: {
type: Sequelize.DataTypes.STRING,
allowNull: false
},
},
{
sequelize,
modelName: 'user',
timestamps: false,
hooks: {
beforeCreate: async (user) => {
const saltRounds = 10;
const salt = await bcrypt.genSalt(saltRounds);
user.password = await bcrypt.hash(user.password, salt);
},
},
}
);
User.prototype.isPasswordValid = async function (password) {
return await bcrypt.compare(password, this.password);
}
module.exports = User;