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"], "plugins": ["@typescript-eslint", "prettier"],
"rules": { "rules": {
"no-console": "off", "no-console": "off",
"no-param-reassign": "off",
"prettier/prettier": "error", "prettier/prettier": "error",
"import/extensions": [ "import/extensions": [
"error", "error",

View File

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

View File

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

View File

@@ -152,6 +152,11 @@
dependencies: dependencies:
defer-to-connect "^1.0.1" 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": "@types/bluebird@^3.5.32":
version "3.5.32" version "3.5.32"
resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.32.tgz#381e7b59e39f010d20bbf7e044e48f5caf1ab620" resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.32.tgz#381e7b59e39f010d20bbf7e044e48f5caf1ab620"