From 38b6cd1a6295ba1b1de85c88aa8aad1f1278f206 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 8 Jan 2025 00:14:11 -0600 Subject: [PATCH 1/2] Refactor HttpExceptionFilter to unify the error response --- .../http-exception/http-exception.filter.ts | 49 +++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/common/filters/http-exception/http-exception.filter.ts b/src/common/filters/http-exception/http-exception.filter.ts index c587769..40f6bb1 100644 --- a/src/common/filters/http-exception/http-exception.filter.ts +++ b/src/common/filters/http-exception/http-exception.filter.ts @@ -5,34 +5,55 @@ import { HttpException, HttpStatus, } from '@nestjs/common'; -import { Response } from 'express'; +import { Request, Response } from 'express'; @Catch() export class HttpExceptionFilter implements ExceptionFilter { - catch(exception: unknown, host: ArgumentsHost) { + catch(exception: unknown, host: ArgumentsHost): void { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const request = ctx.getRequest(); - const status = - exception instanceof HttpException - ? exception.getStatus() - : HttpStatus.INTERNAL_SERVER_ERROR; - const message = - exception instanceof HttpException - ? exception.getResponse() - : 'Internal server error'; + const status = this.getStatus(exception); + const errorMessage = this.getErrorMessage(exception); + const formattedStatus = this.formatStatus(status); const errorResponse = { statusCode: status, timestamp: new Date().toISOString(), path: request.url, - error: message, + error: + typeof errorMessage === 'string' + ? { + message: errorMessage, + error: formattedStatus, + statusCode: status, + } + : errorMessage, }; - // Optionally log the exception - console.error(`Error occurred:`, exception); - + console.error('Error occurred:', exception); response.status(status).json(errorResponse); } + + private getStatus(exception: unknown): HttpStatus { + return exception instanceof HttpException + ? exception.getStatus() + : HttpStatus.INTERNAL_SERVER_ERROR; + } + + private getErrorMessage(exception: unknown): string | object { + return exception instanceof HttpException + ? exception.getResponse() + : 'Internal server error'; + } + + private formatStatus(status: HttpStatus): string { + return HttpStatus[status] + .toLowerCase() + .replace('_', ' ') + .split(' ') + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) + .join(' '); + } } From a8ac2ecae36f18aedbf2bd3aa712283ad8da09fc Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 8 Jan 2025 00:14:21 -0600 Subject: [PATCH 2/2] Refactor and enhance user authentication in AuthService --- libs/common/src/auth/services/auth.service.ts | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/libs/common/src/auth/services/auth.service.ts b/libs/common/src/auth/services/auth.service.ts index 95d592e..bc25e0e 100644 --- a/libs/common/src/auth/services/auth.service.ts +++ b/libs/common/src/auth/services/auth.service.ts @@ -33,32 +33,33 @@ export class AuthService { const user = await this.userRepository.findOne({ where: { email, - region: regionUuid - ? { - uuid: regionUuid, - } - : undefined, + region: regionUuid ? { uuid: regionUuid } : undefined, }, relations: ['roleType'], }); + if (!user) { + throw new BadRequestException('Invalid credentials'); + } + if (!user.isUserVerified) { throw new BadRequestException('User is not verified'); } if (!user.isActive) { throw new BadRequestException('User is not active'); } - if (user) { - const passwordMatch = this.helperHashService.bcryptCompare( - pass, - user.password, - ); - if (passwordMatch) { - const { ...result } = user; - return result; - } + + const passwordMatch = await this.helperHashService.bcryptCompare( + pass, + user.password, + ); + if (!passwordMatch) { + throw new BadRequestException('Invalid credentials'); } - return null; + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { password, ...result } = user; + return result; } async createSession(data): Promise {