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
@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)

@ -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(

Loading…
Cancel
Save