diff --git a/server/src/contexts/auth/domain/entities/AuthUser.ts b/server/src/contexts/auth/domain/entities/AuthUser.ts index b393e10..c1238b6 100644 --- a/server/src/contexts/auth/domain/entities/AuthUser.ts +++ b/server/src/contexts/auth/domain/entities/AuthUser.ts @@ -1,10 +1,9 @@ -import bCrypt from "bcryptjs"; - import { AggregateRoot, Email, IDomainError, Name, + Password, Result, UniqueID, } from "@shared/contexts"; @@ -12,15 +11,14 @@ import { export interface IAuthUserProps { name: Name; email: Email; - password?: string; - hashed_password?: string; + password: Password; } export interface IAuthUser { id: UniqueID; name: Name; email: Email; - hashed_password: string; + password: Password; isUser: boolean; isAdmin: boolean; @@ -35,28 +33,12 @@ export class AuthUser props: IAuthUserProps, id?: UniqueID, ): Result { - //const isNew = !!id === false; - - // Se hace en el constructor de la Entidad - /* if (isNew) { - id = UniqueEntityID.create(); - }*/ - const user = new AuthUser(props, id); - return Result.ok(user); } public static async hashPassword(password): Promise { - return hashPassword(password, await genSalt()); - } - - private _hashed_password: string; - - private constructor(props: IAuthUserProps, id?: UniqueID) { - super({ ...props, password: "", hashed_password: "" }, id); - - this._protectPassword(props); + return Password.hashPassword(password); } get name(): Name { @@ -67,8 +49,8 @@ export class AuthUser return this.props.email; } - get hashed_password(): string { - return this._hashed_password; + get password(): Password { + return this.props.password; } get isUser(): boolean { @@ -80,36 +62,6 @@ export class AuthUser } public verifyPassword(candidatePassword: string): boolean { - return bCrypt.compareSync(candidatePassword, this._hashed_password!); - } - - private async _protectPassword(props: IAuthUserProps) { - const { password, hashed_password } = props; - - if (password) { - this._hashed_password = await AuthUser.hashPassword(password); - } else { - this._hashed_password = hashed_password!; - } + return this.props.password.verifyPassword(candidatePassword); } } - -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); - }); - }); -} - -AuthUser.hashPassword("123456").then((value) => console.log(value)); diff --git a/server/src/contexts/auth/infrastructure/Auth.repository.ts b/server/src/contexts/auth/infrastructure/Auth.repository.ts index 5593fce..e84242d 100644 --- a/server/src/contexts/auth/infrastructure/Auth.repository.ts +++ b/server/src/contexts/auth/infrastructure/Auth.repository.ts @@ -8,7 +8,7 @@ import { Email, ICollection, IQueryCriteria, UniqueID } from "@shared/contexts"; import { Transaction } from "sequelize"; import { AuthUser } from "../domain/entities"; import { IAuthRepository } from "../domain/repository/AuthRepository.interface"; -import { IUserMapper, createUserMapper } from "./mappers/user.mapper"; +import { IUserMapper, createUserMapper } from "./mappers/authuser.mapper"; export type QueryParams = { pagination: Record; diff --git a/server/src/contexts/auth/infrastructure/mappers/user.mapper.ts b/server/src/contexts/auth/infrastructure/mappers/authuser.mapper.ts similarity index 92% rename from server/src/contexts/auth/infrastructure/mappers/user.mapper.ts rename to server/src/contexts/auth/infrastructure/mappers/authuser.mapper.ts index 01d002e..3702d42 100644 --- a/server/src/contexts/auth/infrastructure/mappers/user.mapper.ts +++ b/server/src/contexts/auth/infrastructure/mappers/authuser.mapper.ts @@ -17,7 +17,7 @@ export interface IUserMapper AuthUser > {} -class UserMapper +class AuthUserMapper extends SequelizeMapper implements IUserMapper { @@ -29,7 +29,7 @@ class UserMapper const props: IAuthUserProps = { name: this.mapsValue(source, "name", Name.create), email: this.mapsValue(source, "email", Email.create), - hashed_password: this.mapsValue( + password: this.mapsValue( source, "password", Password.createFromHashedText, @@ -54,12 +54,12 @@ class UserMapper id: source.id.toPrimitive(), name: source.name.toPrimitive(), email: source.email.toPrimitive(), - password: source.hashed_password, + password: source.password.toPrimitive(), }; } } export const createUserMapper = (context: IAuthContext): IUserMapper => - new UserMapper({ + new AuthUserMapper({ context, }); diff --git a/server/src/contexts/auth/infrastructure/mappers/index.ts b/server/src/contexts/auth/infrastructure/mappers/index.ts index adc0092..ef514f3 100644 --- a/server/src/contexts/auth/infrastructure/mappers/index.ts +++ b/server/src/contexts/auth/infrastructure/mappers/index.ts @@ -1 +1 @@ -export * from "./user.mapper"; +export * from "./authuser.mapper"; diff --git a/server/src/contexts/users/domain/entities/User.ts b/server/src/contexts/users/domain/entities/User.ts index 261ff61..de54449 100644 --- a/server/src/contexts/users/domain/entities/User.ts +++ b/server/src/contexts/users/domain/entities/User.ts @@ -1,5 +1,3 @@ -import bCrypt from "bcryptjs"; - import { AggregateRoot, Email, @@ -24,10 +22,9 @@ export interface IUser { id: UniqueID; name: Name; email: Email; + password: Password; isUser: boolean; isAdmin: boolean; - - verifyPassword: (candidatePassword: string) => boolean; } export class User extends AggregateRoot implements IUser { @@ -53,8 +50,6 @@ export class User extends AggregateRoot implements IUser { return Password.hashPassword(password); } - private _password: string; - get name(): Name { return this.props.name; } @@ -63,8 +58,8 @@ export class User extends AggregateRoot implements IUser { return this.props.email; } - get hashed_password(): string { - return this._password; + get password(): Password { + return this.props.password; } get isUser(): boolean { @@ -74,26 +69,4 @@ export class User extends AggregateRoot implements IUser { get isAdmin(): boolean { return true; } - - public verifyPassword(candidatePassword: string): boolean { - return bCrypt.compareSync(candidatePassword, this._password!); - } -} - -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/users/infrastructure/mappers/user.mapper.ts b/server/src/contexts/users/infrastructure/mappers/user.mapper.ts index 6946c7c..5baac1e 100644 --- a/server/src/contexts/users/infrastructure/mappers/user.mapper.ts +++ b/server/src/contexts/users/infrastructure/mappers/user.mapper.ts @@ -47,7 +47,7 @@ class UserMapper id: source.id.toPrimitive(), name: source.name.toPrimitive(), email: source.email.toPrimitive(), - password: "", + password: source.password.toPrimitive(), }; } }