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