From 5d5c1c2eefe2dcc1be7ea877d3cd028bb12fdec8 Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Sat, 11 Jan 2025 15:56:23 +0330 Subject: [PATCH] Enhance error handling in admin module --- src/admin/admin.controller.ts | 10 +++++---- src/admin/admin.service.ts | 41 ++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index fbffe14..f289c16 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -15,7 +15,7 @@ export class AdminController { } //login as admin @Post("login") - async login(@Body() loginAdminDto: LoginAdminDto): Promise<{ accessToken; refreshToken }> { + async login(@Body() loginAdminDto: LoginAdminDto): Promise<{ accessToken }> { return this.adminService.login(loginAdminDto); } //logout user @@ -26,9 +26,11 @@ export class AdminController { return this.adminService.logout(userId); } //get a new access token - @Post("new-token") - async newAccessToken(@Body("refreshToken") refreshToken: string) { - return this.adminService.newAccessToken(refreshToken); + @UseGuards(RoleGuard) + @Get("new-token") + async newAccessToken(@Request() req) { + const userId = req.user.id + return this.adminService.refreshToken(userId); } //edit admin profile @UseGuards(RoleGuard) diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index 8b390c6..b13ebc2 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -34,7 +34,7 @@ export class AdminService { } } //login method - async login(loginAdminDto: LoginAdminDto): Promise<{ accessToken: string; refreshToken: string }> { + async login(loginAdminDto: LoginAdminDto): Promise<{ accessToken: string }> { try { const admin = await this.adminModel.findOne({ where: { email: loginAdminDto.email }, @@ -69,7 +69,7 @@ export class AdminService { { where: { id: admin.id } }, ); - return { accessToken, refreshToken }; + return { accessToken }; } catch (error) { if (error instanceof HttpException) { throw error; @@ -78,38 +78,43 @@ export class AdminService { } } //logout (delete refresh token from database) - async logout(userId: number) { - const user = await this.adminModel.findOne({ - where: { id: userId }, - }); - if (!user) { - throw new HttpException("User not found.", HttpStatus.NOT_FOUND); + async logout(userId: number): Promise<{ message: string }> { + try { + if (!userId) { + throw new HttpException("User ID is required.", HttpStatus.BAD_REQUEST); + } + const user = await this.adminModel.findOne({ where: { id: userId } }); + if (!user) { + throw new HttpException("User not found.", HttpStatus.NOT_FOUND); + } + await this.adminModel.update({ refreshToken: null }, { where: { id: userId } }); + return { message: "Logout is successful" }; + } catch (error) { + throw new HttpException("An unexpected error occurred during logout. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR); } - await this.adminModel.update({ refreshToken: null }, { where: { id: userId } }); - return { message: "logout is successful" }; } //getting new access token - async newAccessToken(refreshToken: string) { + async refreshToken(userId: number): Promise<{ accessToken: string }> { + const refreshToken = (await this.adminModel.findOne({ where: { id: userId } })).refreshToken; + if (!refreshToken) { - throw new HttpException({ message: "Refresh token is required." }, HttpStatus.BAD_REQUEST); + throw new HttpException("Refresh token is required.", HttpStatus.BAD_REQUEST); } let decoded; try { decoded = this.jwtService.verify(refreshToken, { secret: process.env.JWT_REFRESH_SECRET }); } catch (error) { - throw new HttpException({ message: "Invalid or expired token." }, HttpStatus.UNAUTHORIZED); + throw new HttpException("Invalid or expired token.", HttpStatus.UNAUTHORIZED); } - const user = await this.adminModel.findOne({ - where: { id: decoded.id }, - }); + const user = await this.adminModel.findOne({ where: { id: decoded.id } }); if (!user) { - throw new HttpException({ message: "User not found." }, HttpStatus.NOT_FOUND); + throw new HttpException("User not found.", HttpStatus.NOT_FOUND); } if (user.role !== "admin") { - throw new HttpException({ message: "You are not authorized to get an admin token." }, HttpStatus.FORBIDDEN); + throw new HttpException("You are not authorized to get an admin token.", HttpStatus.FORBIDDEN); } const accessToken = this.jwtService.sign(