Upload avatar to s3 on login

This commit is contained in:
Jori Lallo
2017-10-19 00:49:22 -07:00
parent 0515a6233e
commit 255d7564c5
8 changed files with 111 additions and 17 deletions

View File

@ -2,6 +2,7 @@
import crypto from 'crypto';
import bcrypt from 'bcrypt';
import { DataTypes, sequelize, encryptedFields } from '../sequelize';
import { uploadToS3FromUrl } from '../utils/s3';
import JWT from 'jsonwebtoken';
@ -18,6 +19,7 @@ const User = sequelize.define(
email: { type: DataTypes.STRING },
username: { type: DataTypes.STRING },
name: DataTypes.STRING,
avatarUrl: { type: DataTypes.STRING, allowNull: true },
password: DataTypes.VIRTUAL,
passwordDigest: DataTypes.STRING,
isAdmin: DataTypes.BOOLEAN,
@ -66,6 +68,12 @@ User.prototype.verifyPassword = function(password) {
});
});
};
User.prototype.updateAvatar = async function() {
this.avatarUrl = await uploadToS3FromUrl(
this.slackData.image_192,
`avatars/${this.id}`
);
};
const setRandomJwtSecret = model => {
model.jwtSecret = crypto.randomBytes(64).toString('hex');