diff --git a/backend/.eslintrc.json b/backend/.eslintrc.json index 2ebe3c9..17e8e9c 100644 --- a/backend/.eslintrc.json +++ b/backend/.eslintrc.json @@ -17,6 +17,7 @@ "plugins": ["@typescript-eslint", "prettier"], "rules": { "no-console": "off", + "no-param-reassign": "off", "prettier/prettier": "error", "import/extensions": [ "error", diff --git a/backend/package.json b/backend/package.json index e4c5684..46fec36 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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", diff --git a/backend/src/models/User.ts b/backend/src/models/User.ts index 76b71b3..ad08c66 100644 --- a/backend/src/models/User.ts +++ b/backend/src/models/User.ts @@ -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 { - @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 { @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 => { + 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 => { + // return compare(password, instance.passwordHash); // }; } export default User; - -// const bcrypt = require("bcryptjs"); -// @Table -// class User extends Model { -// 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); -// } -// } diff --git a/backend/src/services/CreateUserService.ts b/backend/src/services/CreateUserService.ts index edeeaa7..65363bf 100644 --- a/backend/src/services/CreateUserService.ts +++ b/backend/src/services/CreateUserService.ts @@ -42,7 +42,7 @@ const CreateUserService = async ({ const user = User.create({ email, - passwordHash: password, + password, name, profile }); diff --git a/backend/yarn.lock b/backend/yarn.lock index d498fb3..f19d8b9 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -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"