Loading backend/src/admin/admin.service.ts +4 −0 Original line number Diff line number Diff line Loading @@ -23,4 +23,8 @@ export class AdminService { throw new UnauthorizedException('Admin not found'); } } async updatePassword(email: string, password: string) { await this.adminRepository.update({ email }, { password }); } } backend/src/auth/admin/authAdmin.service.ts +11 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ import { AdminSigninResDto } from './dto/admin-signin-res.dto'; import { Admin } from 'src/admin/entities/admin.entity'; import { ADMIN_ROLE } from 'src/shared/enum/admin-role.enum'; import { PayloadJwtDto } from 'src/shared/dto/payload-jwt.dto'; import { UpdatePwdDto } from '../user/dto/update-pwd.dto'; @Injectable() export class AuthAdminService { Loading Loading @@ -58,4 +59,14 @@ export class AuthAdminService { throw new UnauthorizedException('Invalid token'); } } async changePassword(email: string, updatePwdDto: UpdatePwdDto): Promise<void> { const admin: Admin = await this.adminService.findOne(email); const validPwd: boolean = await this.encryptionService.comparePassword(updatePwdDto.prevPassword, admin.password); if (!validPwd) { throw new HttpException('Invalid password', HttpStatus.UNAUTHORIZED); } const hashedPwd = await this.encryptionService.hashPassword(updatePwdDto.newPassword); await this.adminService.updatePassword(email, hashedPwd); } } backend/src/auth/admin/authAdmincontroller.ts +13 −2 Original line number Diff line number Diff line import { Body, Controller, Post, UseGuards } from '@nestjs/common'; import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common'; import { AuthAdminService } from './authAdmin.service'; import { CreateAdminDto } from 'src/admin/dto/create-admin.dto'; import { LoginAdminDto } from 'src/auth/admin/dto/login-admin.dto'; import { ApiBearerAuth, ApiBody, ApiCreatedResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger'; import { AdminSigninResDto } from './dto/admin-signin-res.dto'; import { Roles } from '../role.decorator'; import { ADMIN_ROLE, SUPERADMIN_ROLES } from 'src/shared/enum/admin-role.enum'; import { ADMIN_ROLE, ADMIN_ROLES, SUPERADMIN_ROLES } from 'src/shared/enum/admin-role.enum'; import { AuthAdminGuard } from './authAdmin.guard'; import { UpdatePwdDto } from '../user/dto/update-pwd.dto'; import { CustomAdminRequest } from './interface/customAdminReq'; @Controller() @ApiTags('Create admin account and sign in as admin') Loading Loading @@ -50,4 +52,13 @@ export class AuthAdminController { throw e; } } @UseGuards(AuthAdminGuard) @Roles(ADMIN_ROLES) @Post('admin/change-password') @ApiBearerAuth('jwt') async changePassword(@Req() req: CustomAdminRequest, @Body() updatePwdDto: UpdatePwdDto) { const email = req.admin.email; return this.authAdminService.changePassword(email, updatePwdDto); } } Loading
backend/src/admin/admin.service.ts +4 −0 Original line number Diff line number Diff line Loading @@ -23,4 +23,8 @@ export class AdminService { throw new UnauthorizedException('Admin not found'); } } async updatePassword(email: string, password: string) { await this.adminRepository.update({ email }, { password }); } }
backend/src/auth/admin/authAdmin.service.ts +11 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ import { AdminSigninResDto } from './dto/admin-signin-res.dto'; import { Admin } from 'src/admin/entities/admin.entity'; import { ADMIN_ROLE } from 'src/shared/enum/admin-role.enum'; import { PayloadJwtDto } from 'src/shared/dto/payload-jwt.dto'; import { UpdatePwdDto } from '../user/dto/update-pwd.dto'; @Injectable() export class AuthAdminService { Loading Loading @@ -58,4 +59,14 @@ export class AuthAdminService { throw new UnauthorizedException('Invalid token'); } } async changePassword(email: string, updatePwdDto: UpdatePwdDto): Promise<void> { const admin: Admin = await this.adminService.findOne(email); const validPwd: boolean = await this.encryptionService.comparePassword(updatePwdDto.prevPassword, admin.password); if (!validPwd) { throw new HttpException('Invalid password', HttpStatus.UNAUTHORIZED); } const hashedPwd = await this.encryptionService.hashPassword(updatePwdDto.newPassword); await this.adminService.updatePassword(email, hashedPwd); } }
backend/src/auth/admin/authAdmincontroller.ts +13 −2 Original line number Diff line number Diff line import { Body, Controller, Post, UseGuards } from '@nestjs/common'; import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common'; import { AuthAdminService } from './authAdmin.service'; import { CreateAdminDto } from 'src/admin/dto/create-admin.dto'; import { LoginAdminDto } from 'src/auth/admin/dto/login-admin.dto'; import { ApiBearerAuth, ApiBody, ApiCreatedResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger'; import { AdminSigninResDto } from './dto/admin-signin-res.dto'; import { Roles } from '../role.decorator'; import { ADMIN_ROLE, SUPERADMIN_ROLES } from 'src/shared/enum/admin-role.enum'; import { ADMIN_ROLE, ADMIN_ROLES, SUPERADMIN_ROLES } from 'src/shared/enum/admin-role.enum'; import { AuthAdminGuard } from './authAdmin.guard'; import { UpdatePwdDto } from '../user/dto/update-pwd.dto'; import { CustomAdminRequest } from './interface/customAdminReq'; @Controller() @ApiTags('Create admin account and sign in as admin') Loading Loading @@ -50,4 +52,13 @@ export class AuthAdminController { throw e; } } @UseGuards(AuthAdminGuard) @Roles(ADMIN_ROLES) @Post('admin/change-password') @ApiBearerAuth('jwt') async changePassword(@Req() req: CustomAdminRequest, @Body() updatePwdDto: UpdatePwdDto) { const email = req.admin.email; return this.authAdminService.changePassword(email, updatePwdDto); } }