change user model to typescript

This commit is contained in:
canove
2020-09-14 16:47:43 -03:00
parent 60b12cbc9f
commit eba3553a2d
5 changed files with 27 additions and 48 deletions

View File

@@ -17,6 +17,7 @@
"plugins": ["@typescript-eslint", "prettier"],
"rules": {
"no-console": "off",
"no-param-reassign": "off",
"prettier/prettier": "error",
"import/extensions": [
"error",

View File

@@ -30,6 +30,7 @@
"yup": "^0.29.3"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/bluebird": "^3.5.32",
"@types/cors": "^2.8.7",
"@types/express": "^4.17.8",

View File

@@ -4,24 +4,23 @@ import {
CreatedAt,
UpdatedAt,
Model,
DataType
DataType,
BeforeCreate,
BeforeUpdate
} from "sequelize-typescript";
import { hash, compare } from "bcryptjs";
@Table
class User extends Model<User> {
@Column({
defaultValue: DataType.UUIDV4,
primaryKey: true,
type: DataType.UUID
})
id: string;
@Column
name: string;
@Column
email: string;
@Column(DataType.VIRTUAL)
password: string;
@Column
passwordHash: string;
@@ -36,47 +35,20 @@ class User extends Model<User> {
@UpdatedAt
updatedAt: Date;
// @BeforeUpdate
// @BeforeInsert
// hashPassword = async () => {
// if (this.passwordHash) {
// this.passwordHash = await hash(this.passwordHash, 8);
// }
// };
@BeforeUpdate
@BeforeCreate
static hashPassword = async (instance: User): Promise<void> => {
if (instance.password) {
instance.passwordHash = await hash(instance.password, 8);
}
};
// checkPassword = async (password: string) => {
// return await compare(password, this.passwordHash);
// static checkPassword = async ( // maybe not work like this.
// instance: User,
// password: string
// ): Promise<boolean> => {
// return compare(password, instance.passwordHash);
// };
}
export default User;
// const bcrypt = require("bcryptjs");
// @Table
// class User extends Model<User> {
// static init(sequelize) {
// super.init(
// {
// name: { type: Sequelize.STRING },
// password: { type: Sequelize.VIRTUAL },
// profile: { type: Sequelize.STRING, defaultValue: "admin" },
// passwordHash: { type: Sequelize.STRING },
// email: { type: Sequelize.STRING }
// },
// {
// sequelize
// }
// );
// this.addHook("beforeSave", async user => {
// if (user.password) {
// user.passwordHash = await bcrypt.hash(user.password, 8);
// }
// });
// return this;
// }
// checkPassword(password) {
// return bcrypt.compare(password, this.passwordHash);
// }
// }

View File

@@ -42,7 +42,7 @@ const CreateUserService = async ({
const user = User.create({
email,
passwordHash: password,
password,
name,
profile
});

View File

@@ -152,6 +152,11 @@
dependencies:
defer-to-connect "^1.0.1"
"@types/bcryptjs@^2.4.2":
version "2.4.2"
resolved "https://registry.yarnpkg.com/@types/bcryptjs/-/bcryptjs-2.4.2.tgz#e3530eac9dd136bfdfb0e43df2c4c5ce1f77dfae"
integrity sha512-LiMQ6EOPob/4yUL66SZzu6Yh77cbzJFYll+ZfaPiPPFswtIlA/Fs1MzdKYA7JApHU49zQTbJGX3PDmCpIdDBRQ==
"@types/bluebird@^3.5.32":
version "3.5.32"
resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.32.tgz#381e7b59e39f010d20bbf7e044e48f5caf1ab620"