Files
yourpart3/backend/models/community/user.js
2024-07-22 18:14:12 +02:00

64 lines
1.5 KiB
JavaScript

import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
import bcrypt from 'bcrypt';
import { encrypt, generateIv } from '../../utils/encryption.js';
import crypto from 'crypto';
const User = sequelize.define('user', {
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
set(value) {
if (value) {
const iv = generateIv();
this.setDataValue('iv', iv.toString('hex'));
this.setDataValue('email', encrypt(value, iv));
}
}
},
iv: {
type: DataTypes.STRING,
allowNull: false
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
registrationDate: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW
},
active: {
type: DataTypes.BOOLEAN,
defaultValue: false
},
resetToken: {
type: DataTypes.UUID,
allowNull: true
},
hashedId: {
type: DataTypes.STRING,
allowNull: true
}
}, {
tableName: 'user',
schema: 'community',
underscored: true,
hooks: {
afterCreate: async (user, options) => {
const hashedId = crypto.createHash('sha256').update(user.id.toString()).digest('hex');
user.hashedId = hashedId;
await user.save();
}
}
});
export default User;