Commit bf39fa95 authored by Lorenzo Trujillo Rojas's avatar Lorenzo Trujillo Rojas
Browse files

Merge branch 'main' into 'main'

Agregando endpoint para actualizar contraseña de un usuario y de un admin

See merge request ltrpro/pueblosmagicosconia!43
parents 37886f99 0bf8777f
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
import { Controller, Get, Req } from '@nestjs/common';
import { Controller, Get, Req, UseGuards } from '@nestjs/common';
import { AdminService } from './admin.service';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { ADMIN_ROLES } from 'src/shared/enum/admin-role.enum';
import { Roles } from 'src/auth/role.decorator';
import { CustomAdminRequest } from 'src/auth/admin/interface/customAdminReq';
import { AuthAdminGuard } from 'src/auth/admin/authAdmin.guard';

@Controller('')
@ApiTags('Admin')
export class AdminController {
  constructor(private readonly adminService: AdminService) {}

  @UseGuards(AuthAdminGuard)
  @Roles(ADMIN_ROLES)
  @ApiBearerAuth('jwt')
  @Get('admin/whoami')
+4 −1
Original line number Diff line number Diff line
@@ -4,10 +4,13 @@ import { AdminController } from './admin.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Admin } from './entities/admin.entity';
import { Town } from 'src/town/entities/town.entity';
import { AuthAdminService } from 'src/auth/admin/authAdmin.service';
import { JwtService } from '@nestjs/jwt';
import { EncryptionService } from 'src/auth/encryption/encryption.service';

@Module({
  controllers: [AdminController],
  providers: [AdminService],
  providers: [AdminService, AuthAdminService, JwtService, EncryptionService],
  imports: [TypeOrmModule.forFeature([Admin, Town])],
  exports: [AdminService],
})
+4 −0
Original line number Diff line number Diff line
@@ -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 });
  }
}
+1 −1
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ import { PlaceTraduction } from './place/entities/place-traduction.entity';
    PointOfInterestModule,
  ],
  controllers: [AppController],
  providers: [AppService, DatabaseSeederModule, { provide: APP_GUARD, useClass: AuthAdminGuard }],
  providers: [AppService, DatabaseSeederModule],
  exports: [TypeOrmModule],
})
export class AppModule {}
+11 −0
Original line number Diff line number Diff line
@@ -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 {
@@ -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);
  }
}
Loading