User model cleanup

This commit is contained in:
Tom Moor
2018-06-04 15:08:29 -04:00
parent 1c081f8777
commit 874235eb0f

View File

@ -7,7 +7,7 @@ import { DataTypes, sequelize, encryptedFields } from '../sequelize';
import { publicS3Endpoint, uploadToS3FromUrl } from '../utils/s3';
import { sendEmail } from '../mailer';
const BCRYPT_COST = process.env.NODE_ENV !== 'production' ? 4 : 12;
const BCRYPT_COST = process.env.NODE_ENV === 'production' ? 12 : 4;
const User = sequelize.define(
'user',
@ -24,8 +24,7 @@ const User = sequelize.define(
password: DataTypes.VIRTUAL,
passwordDigest: DataTypes.STRING,
isAdmin: DataTypes.BOOLEAN,
slackAccessToken: encryptedFields.vault('slackAccessToken'),
service: { type: DataTypes.STRING, allowNull: true, unique: true },
service: { type: DataTypes.STRING, allowNull: true },
serviceId: { type: DataTypes.STRING, allowNull: true, unique: true },
slackData: DataTypes.JSONB,
jwtSecret: encryptedFields.vault('jwtSecret'),
@ -91,6 +90,7 @@ const uploadAvatar = async model => {
const setRandomJwtSecret = model => {
model.jwtSecret = crypto.randomBytes(64).toString('hex');
};
const hashPassword = model => {
if (!model.password) {
return null;
@ -108,6 +108,7 @@ const hashPassword = model => {
});
});
};
User.beforeCreate(hashPassword);
User.beforeUpdate(hashPassword);
User.beforeSave(uploadAvatar);