create-ex-den-kit
Version:
Create exam projects with Ex-Den-Kit
63 lines (58 loc) • 1.35 kB
JavaScript
import { DataTypes } from 'sequelize';
import sequelize from '../config/database.js';
import bcrypt from 'bcryptjs';
const User = sequelize.define('User', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
login: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
len: [6, 50],
is: /^[а-яА-Яa-zA-Z0-9]+$/i,
},
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
fullName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
is: /^[а-яА-Я\s]+$/i,
},
},
phone: {
type: DataTypes.STRING,
allowNull: false,
validate: {
is: /^\+7\(\d{3}\)-\d{3}-\d{2}-\d{2}$/,
},
},
email: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isEmail: true,
},
},
role: {
type: DataTypes.ENUM('user', 'admin'),
defaultValue: 'user',
},
});
// Хук для хеширования пароля перед сохранением
User.beforeCreate(async (user) => {
const hashedPassword = await bcrypt.hash(user.password, 10);
user.password = hashedPassword;
});
// Метод для проверки пароля
User.prototype.validatePassword = async function(password) {
return bcrypt.compare(password, this.password);
};
export default User;