From 8347a96cae17738f3224e463dbcd91c56ff8bf81 Mon Sep 17 00:00:00 2001 From: rajdip-b Date: Fri, 24 May 2024 11:31:03 +0530 Subject: [PATCH] organized code --- src/app/app.module.ts | 2 ++ src/auth/controller/auth.controller.ts | 5 ++++- src/auth/dto/email-verification.dto.ts | 2 ++ src/auth/service/auth.service.ts | 5 ----- src/common/common.module.ts | 5 +++++ src/common/pipes/lowercase.pipe.ts | 8 ++++++++ 6 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 src/common/common.module.ts create mode 100644 src/common/pipes/lowercase.pipe.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index ea4e5d4..469523b 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -9,12 +9,14 @@ import { AuthGuard } from '../auth/guard/auth/auth.guard'; import { PrismaModule } from '../prisma/prisma.module'; import { MailModule } from '../mail/mail.module'; import { ProviderModule } from '../provider/provider.module'; +import { CommonModule } from '../common/common.module'; @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, }), + CommonModule, AuthModule, UserModule, OauthModule, diff --git a/src/auth/controller/auth.controller.ts b/src/auth/controller/auth.controller.ts index 3cd3932..b4ce049 100644 --- a/src/auth/controller/auth.controller.ts +++ b/src/auth/controller/auth.controller.ts @@ -32,6 +32,7 @@ import { ApiTags, } from '@nestjs/swagger'; import { userProperties } from '../../schemas/user.properties'; +import { LowercasePipe } from 'src/common/pipes/lowercase.pipe'; @Controller('auth') @ApiTags('Auth Controller') @@ -195,7 +196,9 @@ export class AuthController { description: 'User not found', }) @HttpCode(HttpStatus.NO_CONTENT) - async resendEmailVerificationCode(@Param('email') email: string) { + async resendEmailVerificationCode( + @Param('email', LowercasePipe) email: string, + ) { return await this.authService.resendEmailVerificationCode(email); } diff --git a/src/auth/dto/email-verification.dto.ts b/src/auth/dto/email-verification.dto.ts index d76f80d..561e663 100644 --- a/src/auth/dto/email-verification.dto.ts +++ b/src/auth/dto/email-verification.dto.ts @@ -1,4 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; +import { Transform } from 'class-transformer'; import { IsEmail, Matches } from 'class-validator'; export class EmailVerificationDto { @@ -10,6 +11,7 @@ export class EmailVerificationDto { type: String, example: 'johndoe@example.com', }) + @Transform(({ value }) => value.toLowerCase()) email: string; @Matches(/^[0-9]{6}$/, { message: 'Code must be a 6 digit number', diff --git a/src/auth/service/auth.service.ts b/src/auth/service/auth.service.ts index a5dc8a8..8275fb2 100644 --- a/src/auth/service/auth.service.ts +++ b/src/auth/service/auth.service.ts @@ -131,7 +131,6 @@ export class AuthService { } async resendEmailVerificationCode(email: string) { - email = email.toLowerCase(); const user = await this.findUserByEmail(email); if (!user) { throw new NotFoundException('User not found'); @@ -144,8 +143,6 @@ export class AuthService { } async verifyEmail(email: string, code: string) { - email = email.toLowerCase(); - const verificationCode = await this.prisma.verificationCode.findUnique({ where: { email, @@ -262,8 +259,6 @@ export class AuthService { } private async sendEmailVerificationCode(email: string) { - email = email.toLowerCase(); - // Generate the code let code: string; diff --git a/src/common/common.module.ts b/src/common/common.module.ts new file mode 100644 index 0000000..b7bd955 --- /dev/null +++ b/src/common/common.module.ts @@ -0,0 +1,5 @@ +import { Global, Module } from '@nestjs/common'; + +@Global() +@Module({}) +export class CommonModule {} diff --git a/src/common/pipes/lowercase.pipe.ts b/src/common/pipes/lowercase.pipe.ts new file mode 100644 index 0000000..317bd0b --- /dev/null +++ b/src/common/pipes/lowercase.pipe.ts @@ -0,0 +1,8 @@ +import { PipeTransform, Injectable } from '@nestjs/common'; + +@Injectable() +export class LowercasePipe implements PipeTransform { + transform(value: string) { + return value.toLowerCase(); + } +}