2017-07-12 07:28:18 +00:00
|
|
|
// @flow
|
2016-04-29 05:25:37 +00:00
|
|
|
import crypto from 'crypto';
|
2016-09-12 00:47:22 +00:00
|
|
|
import bcrypt from 'bcrypt';
|
2017-04-27 04:47:03 +00:00
|
|
|
import { DataTypes, sequelize, encryptedFields } from '../sequelize';
|
2016-04-29 05:25:37 +00:00
|
|
|
|
|
|
|
import JWT from 'jsonwebtoken';
|
|
|
|
|
2016-09-12 00:47:22 +00:00
|
|
|
const BCRYPT_COST = process.env.NODE_ENV !== 'production' ? 4 : 12;
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
const User = sequelize.define(
|
|
|
|
'user',
|
|
|
|
{
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true,
|
2016-04-29 05:25:37 +00:00
|
|
|
},
|
2017-07-12 07:28:18 +00:00
|
|
|
email: { type: DataTypes.STRING },
|
|
|
|
username: { type: DataTypes.STRING },
|
2017-04-27 04:47:03 +00:00
|
|
|
name: DataTypes.STRING,
|
|
|
|
password: DataTypes.VIRTUAL,
|
|
|
|
passwordDigest: DataTypes.STRING,
|
|
|
|
isAdmin: DataTypes.BOOLEAN,
|
|
|
|
slackAccessToken: encryptedFields.vault('slackAccessToken'),
|
2017-07-12 07:28:18 +00:00
|
|
|
slackId: { type: DataTypes.STRING, allowNull: true, unique: true },
|
2017-04-27 04:47:03 +00:00
|
|
|
slackData: DataTypes.JSONB,
|
|
|
|
jwtSecret: encryptedFields.vault('jwtSecret'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: ['email'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
);
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2017-07-12 07:28:18 +00:00
|
|
|
// Class methods
|
|
|
|
User.associate = models => {
|
|
|
|
User.hasMany(models.ApiKey, { as: 'apiKeys' });
|
|
|
|
User.hasMany(models.Document, { as: 'documents' });
|
|
|
|
User.hasMany(models.View, { as: 'views' });
|
|
|
|
};
|
|
|
|
|
|
|
|
// Instance methods
|
|
|
|
User.prototype.getJwtToken = function() {
|
|
|
|
return JWT.sign({ id: this.id }, this.jwtSecret);
|
|
|
|
};
|
|
|
|
User.prototype.getTeam = async function() {
|
|
|
|
return this.team;
|
|
|
|
};
|
|
|
|
User.prototype.verifyPassword = function(password) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!this.passwordDigest) {
|
|
|
|
resolve(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bcrypt.compare(password, this.passwordDigest, (err, ok) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(ok);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
const setRandomJwtSecret = model => {
|
2016-04-29 05:25:37 +00:00
|
|
|
model.jwtSecret = crypto.randomBytes(64).toString('hex');
|
|
|
|
};
|
2016-09-12 00:47:22 +00:00
|
|
|
const hashPassword = function hashPassword(model) {
|
|
|
|
if (!model.password) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
bcrypt.hash(model.password, BCRYPT_COST, (err, digest) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2016-09-12 00:47:22 +00:00
|
|
|
model.passwordDigest = digest;
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
User.beforeCreate(hashPassword);
|
|
|
|
User.beforeUpdate(hashPassword);
|
2016-04-29 05:25:37 +00:00
|
|
|
User.beforeCreate(setRandomJwtSecret);
|
|
|
|
|
|
|
|
export default User;
|