diff --git a/server/package.json b/server/package.json index efea801..eac72fb 100644 --- a/server/package.json +++ b/server/package.json @@ -16,7 +16,7 @@ "author": "Rodax Software ", "license": "ISC", "devDependencies": { - "@types/bcryptjs": "^2.4.6", + "@types/bcrypt": "^5.0.2", "@types/cors": "^2.8.13", "@types/dinero.js": "^1.9.1", "@types/express": "^4.17.21", @@ -57,7 +57,7 @@ "dependencies": { "@joi/date": "^2.1.0", "@reis/joi-luxon": "^3.0.0", - "bcryptjs": "^2.4.3", + "bcrypt": "^5.1.1", "cls-rtracer": "^2.6.3", "cors": "^2.8.5", "cross-env": "5.0.5", diff --git a/server/src/contexts/auth/domain/entities/AuthUser.ts b/server/src/contexts/auth/domain/entities/AuthUser.ts index c1238b6..0911830 100644 --- a/server/src/contexts/auth/domain/entities/AuthUser.ts +++ b/server/src/contexts/auth/domain/entities/AuthUser.ts @@ -1,9 +1,9 @@ +import { Password } from "@/contexts/common/domain"; import { AggregateRoot, Email, IDomainError, Name, - Password, Result, UniqueID, } from "@shared/contexts"; diff --git a/server/src/contexts/auth/infrastructure/mappers/authuser.mapper.ts b/server/src/contexts/auth/infrastructure/mappers/authuser.mapper.ts index 3702d42..794f3c0 100644 --- a/server/src/contexts/auth/infrastructure/mappers/authuser.mapper.ts +++ b/server/src/contexts/auth/infrastructure/mappers/authuser.mapper.ts @@ -1,8 +1,9 @@ +import { Password } from "@/contexts/common/domain"; import { ISequelizeMapper, SequelizeMapper, } from "@/contexts/common/infrastructure"; -import { Email, Name, Password, UniqueID } from "@shared/contexts"; +import { Email, Name, UniqueID } from "@shared/contexts"; import { AuthUser, IAuthUserProps } from "../../domain/entities"; import { IAuthContext } from "../Auth.context"; import { @@ -32,7 +33,7 @@ class AuthUserMapper password: this.mapsValue( source, "password", - Password.createFromHashedText, + Password.createFromHashedTextPassword, ), }; diff --git a/shared/lib/contexts/common/domain/entities/Password.ts b/server/src/contexts/common/domain/entities/Password.ts similarity index 61% rename from shared/lib/contexts/common/domain/entities/Password.ts rename to server/src/contexts/common/domain/entities/Password.ts index 4c6f085..b3673bd 100644 --- a/shared/lib/contexts/common/domain/entities/Password.ts +++ b/server/src/contexts/common/domain/entities/Password.ts @@ -1,14 +1,15 @@ -import bCrypt from "bcryptjs"; +import { + DomainError, + IStringValueObjectOptions, + Result, + RuleValidator, + StringValueObject, + handleDomainError, +} from "@shared/contexts"; +import { UndefinedOr } from "@shared/utilities"; +import bCrypt from "bcrypt"; import Joi from "joi"; -import { UndefinedOr } from "../../../../utilities"; -import { RuleValidator } from "../RuleValidator"; -import { DomainError, handleDomainError } from "../errors"; -import { Result } from "./Result"; -import { - IStringValueObjectOptions, - StringValueObject, -} from "./StringValueObject"; export interface IPasswordOptions extends IStringValueObjectOptions {} @@ -32,7 +33,7 @@ export class Password extends StringValueObject { return RuleValidator.validate(rule, value); } - public static createFromHashedText( + public static createFromHashedTextPassword( value: UndefinedOr, options: IPasswordOptions = {}, ) { @@ -56,7 +57,7 @@ export class Password extends StringValueObject { return Result.ok(new Password(validationResult.object)); } - public static async createFromPlainText( + public static async createFromPlainTextPassword( value: UndefinedOr, options: IPasswordOptions = {}, ) { @@ -82,29 +83,11 @@ export class Password extends StringValueObject { ); } - public static async hashPassword(plainText: string): Promise { - return hashPassword(plainText, await genSalt()); + public static hashPassword(plainTextPassword: string): string { + return bCrypt.hashSync(plainTextPassword, 10); } - public verifyPassword(candidatePassword: string): boolean { - return bCrypt.compareSync(candidatePassword, this.value!); + public verifyPassword(plainTextPassword: string): boolean { + return bCrypt.compareSync(plainTextPassword, this.value!); } } - -async function genSalt(rounds = 10): Promise { - return new Promise((resolve, reject) => { - bCrypt.genSalt(rounds, function (err, salt) { - if (err) return reject(err); - return resolve(salt); - }); - }); -} - -async function hashPassword(password: string, salt: string): Promise { - return new Promise((resolve, reject) => { - bCrypt.hash(password, salt, function (err, hash) { - if (err) return reject(err); - return resolve(hash); - }); - }); -} diff --git a/server/src/contexts/common/domain/entities/index.ts b/server/src/contexts/common/domain/entities/index.ts new file mode 100644 index 0000000..da6c715 --- /dev/null +++ b/server/src/contexts/common/domain/entities/index.ts @@ -0,0 +1 @@ +export * from "./Password"; diff --git a/server/src/contexts/common/domain/index.ts b/server/src/contexts/common/domain/index.ts index 42289fe..ff240c8 100644 --- a/server/src/contexts/common/domain/index.ts +++ b/server/src/contexts/common/domain/index.ts @@ -1,3 +1,4 @@ export * from "./Mapper.interface"; export * from "./Specification"; +export * from "./entities"; export * from "./repositories"; diff --git a/server/src/contexts/users/domain/entities/User.ts b/server/src/contexts/users/domain/entities/User.ts index de54449..a5cbf8f 100644 --- a/server/src/contexts/users/domain/entities/User.ts +++ b/server/src/contexts/users/domain/entities/User.ts @@ -1,9 +1,9 @@ +import { Password } from "@/contexts/common/domain"; import { AggregateRoot, Email, IDomainError, Name, - Password, Result, UniqueID, handleDomainError, diff --git a/server/src/contexts/users/infrastructure/mappers/user.mapper.ts b/server/src/contexts/users/infrastructure/mappers/user.mapper.ts index 5baac1e..a6badc3 100644 --- a/server/src/contexts/users/infrastructure/mappers/user.mapper.ts +++ b/server/src/contexts/users/infrastructure/mappers/user.mapper.ts @@ -1,8 +1,9 @@ +import { Password } from "@/contexts/common/domain"; import { ISequelizeMapper, SequelizeMapper, } from "@/contexts/common/infrastructure"; -import { Email, Name, Password, UniqueID } from "@shared/contexts"; +import { Email, Name, UniqueID } from "@shared/contexts"; import { IUserProps, User } from "../../domain"; import { IUserContext } from "../User.context"; import { TCreationUser_Attributes, User_Model } from "../sequelize/user.model"; @@ -25,7 +26,7 @@ class UserMapper password: this.mapsValue( source, "password", - Password.createFromHashedText, + Password.createFromHashedTextPassword, ), }; diff --git a/shared/lib/contexts/common/domain/entities/index.ts b/shared/lib/contexts/common/domain/entities/index.ts index 210eb2d..569c43e 100644 --- a/shared/lib/contexts/common/domain/entities/index.ts +++ b/shared/lib/contexts/common/domain/entities/index.ts @@ -11,7 +11,6 @@ export * from "./MoneyValue"; export * from "./Name"; export * from "./Note"; export * from "./NullableValueObject"; -export * from "./Password"; export * from "./Percentage"; export * from "./Phone"; export * from "./Quantity";