const { DataTypes } = require('sequelize'); const sequelize = require('../config/database'); const bcrypt = require('bcryptjs'); const User = sequelize.define('User', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true }, username: { type: DataTypes.STRING, unique: true, allowNull: false }, email: { type: DataTypes.STRING, unique: true, allowNull: false, validate: { isEmail: true } }, password: { type: DataTypes.STRING, allowNull: false }, firstName: { type: DataTypes.STRING, allowNull: false }, lastName: { type: DataTypes.STRING, allowNull: false }, phone: { type: DataTypes.STRING }, address: { type: DataTypes.TEXT }, profileImage: { type: DataTypes.STRING }, isVerified: { type: DataTypes.BOOLEAN, defaultValue: false } }, { hooks: { beforeCreate: async (user) => { user.password = await bcrypt.hash(user.password, 10); }, beforeUpdate: async (user) => { if (user.changed('password')) { user.password = await bcrypt.hash(user.password, 10); } } } }); User.prototype.comparePassword = async function(password) { return bcrypt.compare(password, this.password); }; module.exports = User;