Enhance error handling in admin module

master
nicekid1 1 month ago
parent 7d8998b2f1
commit 5d5c1c2eef
  1. 10
      src/admin/admin.controller.ts
  2. 41
      src/admin/admin.service.ts

@ -15,7 +15,7 @@ export class AdminController {
} }
//login as admin //login as admin
@Post("login") @Post("login")
async login(@Body() loginAdminDto: LoginAdminDto): Promise<{ accessToken; refreshToken }> { async login(@Body() loginAdminDto: LoginAdminDto): Promise<{ accessToken }> {
return this.adminService.login(loginAdminDto); return this.adminService.login(loginAdminDto);
} }
//logout user //logout user
@ -26,9 +26,11 @@ export class AdminController {
return this.adminService.logout(userId); return this.adminService.logout(userId);
} }
//get a new access token //get a new access token
@Post("new-token") @UseGuards(RoleGuard)
async newAccessToken(@Body("refreshToken") refreshToken: string) { @Get("new-token")
return this.adminService.newAccessToken(refreshToken); async newAccessToken(@Request() req) {
const userId = req.user.id
return this.adminService.refreshToken(userId);
} }
//edit admin profile //edit admin profile
@UseGuards(RoleGuard) @UseGuards(RoleGuard)

@ -34,7 +34,7 @@ export class AdminService {
} }
} }
//login method //login method
async login(loginAdminDto: LoginAdminDto): Promise<{ accessToken: string; refreshToken: string }> { async login(loginAdminDto: LoginAdminDto): Promise<{ accessToken: string }> {
try { try {
const admin = await this.adminModel.findOne({ const admin = await this.adminModel.findOne({
where: { email: loginAdminDto.email }, where: { email: loginAdminDto.email },
@ -69,7 +69,7 @@ export class AdminService {
{ where: { id: admin.id } }, { where: { id: admin.id } },
); );
return { accessToken, refreshToken }; return { accessToken };
} catch (error) { } catch (error) {
if (error instanceof HttpException) { if (error instanceof HttpException) {
throw error; throw error;
@ -78,38 +78,43 @@ export class AdminService {
} }
} }
//logout (delete refresh token from database) //logout (delete refresh token from database)
async logout(userId: number) { async logout(userId: number): Promise<{ message: string }> {
const user = await this.adminModel.findOne({ try {
where: { id: userId }, if (!userId) {
}); throw new HttpException("User ID is required.", HttpStatus.BAD_REQUEST);
if (!user) { }
throw new HttpException("User not found.", HttpStatus.NOT_FOUND); 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 //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) { 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; let decoded;
try { try {
decoded = this.jwtService.verify(refreshToken, { secret: process.env.JWT_REFRESH_SECRET }); decoded = this.jwtService.verify(refreshToken, { secret: process.env.JWT_REFRESH_SECRET });
} catch (error) { } 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({ const user = await this.adminModel.findOne({ where: { id: decoded.id } });
where: { id: decoded.id },
});
if (!user) { 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") { 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( const accessToken = this.jwtService.sign(

Loading…
Cancel
Save