From aa44e3c2fff2cdacbad8e16702bb6855f47bd607 Mon Sep 17 00:00:00 2001 From: Carlos Gutierrez Date: Sat, 15 Mar 2025 18:14:14 -0400 Subject: [PATCH] fix: Using nestJS Serialization to transform and sanitizing the data returned at sign-up --- src/auth/auth.service.ts | 12 +++--------- src/auth/dto/sign-up.dto.ts | 6 ------ src/user/entities/user.entity.ts | 2 ++ 3 files changed, 5 insertions(+), 15 deletions(-) delete mode 100644 src/auth/dto/sign-up.dto.ts diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 3d6effb..ed0fc4f 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -4,11 +4,12 @@ import { UnauthorizedException, BadRequestException, } from '@nestjs/common'; +import { plainToInstance } from 'class-transformer'; import { UserService } from 'src/user/user.service'; import { LoginDTO, LoginResponseDTO } from './dto/login.dto'; -import { UserSignUpResponseDTO } from './dto/sign-up.dto'; import { UserDTO } from 'src/user/dto/user.dto'; import { JwtService } from '@nestjs/jwt'; +import { User } from 'src/user/entities/user.entity'; @Injectable() export class AuthService { @@ -47,13 +48,6 @@ export class AuthService { const newUser = await this.userService.create(newUserData); - const userCreated: UserSignUpResponseDTO = { - firstName: newUser.firstName, - lastName: newUser.lastName, - email: newUser.email, - documentId: newUser.documentId, - phoneNumber: newUser.phoneNumber, - }; - return userCreated; + return plainToInstance(User, newUser); } } diff --git a/src/auth/dto/sign-up.dto.ts b/src/auth/dto/sign-up.dto.ts deleted file mode 100644 index 0e332f3..0000000 --- a/src/auth/dto/sign-up.dto.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { OmitType } from '@nestjs/swagger'; -import { UserDTO } from 'src/user/dto/user.dto'; - -export class UserSignUpResponseDTO extends OmitType(UserDTO, [ - 'password', -] as const) {} diff --git a/src/user/entities/user.entity.ts b/src/user/entities/user.entity.ts index 9627e5a..9274f72 100644 --- a/src/user/entities/user.entity.ts +++ b/src/user/entities/user.entity.ts @@ -1,5 +1,6 @@ import { BaseModel } from 'src/utils/entity'; import { Entity, Column } from 'typeorm'; +import { Exclude } from 'class-transformer'; export enum UserRole { ADMIN = 'admin', @@ -16,6 +17,7 @@ export class User extends BaseModel { @Column({ type: 'character varying', name: 'last_name' }) lastName: string; + @Exclude() @Column({ type: 'character varying' }) password: string;