39 lines
826 B
JavaScript
39 lines
826 B
JavaScript
![]() |
const { DataTypes } = require('sequelize');
|
||
|
const { sequelize } = require('../config/db');
|
||
|
const bcrypt = require('bcrypt');
|
||
|
|
||
|
const User = sequelize.define('User', {
|
||
|
id: {
|
||
|
type: DataTypes.INTEGER,
|
||
|
primaryKey: true,
|
||
|
autoIncrement: true
|
||
|
},
|
||
|
username: {
|
||
|
type: DataTypes.STRING,
|
||
|
allowNull: false,
|
||
|
unique: true
|
||
|
},
|
||
|
email: {
|
||
|
type: DataTypes.STRING,
|
||
|
allowNull: false,
|
||
|
unique: true,
|
||
|
validate: {
|
||
|
isEmail: true
|
||
|
}
|
||
|
},
|
||
|
password: {
|
||
|
type: DataTypes.STRING,
|
||
|
allowNull: false
|
||
|
}
|
||
|
}, {
|
||
|
hooks: {
|
||
|
beforeCreate: async (user) => {
|
||
|
const salt = await bcrypt.genSalt(10);
|
||
|
user.password = await bcrypt.hash(user.password, salt);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
User.prototype.validatePassword = async function(password) {
|
||
|
return await bcrypt.compare(password, this.password);
|
||
|
};
|