diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 3c8ee7c..704de9a 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -7,10 +7,10 @@ import { UnauthorizedException, } from '@nestjs/common'; import { AuthService } from './auth.service'; - + @Controller('auth') export class AuthController { - constructor(private readonly authService: AuthService) {} + constructor(private readonly authService: AuthService) { } // ===================== LOGIN ===================== @Post('login') @@ -36,4 +36,15 @@ export class AuthController { throw new UnauthorizedException(error.message); } } + + // ===================== ACTUALIZAR CONTRASEÑA ADMIN ===================== + @Post('update-password') + async updatePassword() { + try { + const newPassword = await this.authService.update_password(); + return { message: 'Contraseña actualizada correctamente.', contraseña: newPassword }; + } catch (error) { + throw new UnauthorizedException(error.message); + } + } } diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index e127f1d..ca1e84e 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -73,6 +73,20 @@ export class AuthService { return password; } + async update_password() { + let user = await this.userRepo.findOne({ where: { tipoUsuario: { idTipoUsuario: 1 } } }) + + if (!user) throw new NotFoundException('No se encontró el usuario administrador.'); + const newPassword = await this.generarPassword(); + const hashedPassword = await this.encriptar(newPassword); + user.password = hashedPassword; + await this.userRepo.save(user); + return newPassword; + } + + + + async login(usuario: string, password: string) { const user = await this.userRepo.findOne({ where: { usuario }, diff --git a/src/usuario/usuario.controller.ts b/src/usuario/usuario.controller.ts index 5d7275b..5aa3b51 100644 --- a/src/usuario/usuario.controller.ts +++ b/src/usuario/usuario.controller.ts @@ -91,6 +91,11 @@ export class UsuarioController { ); } + @Get('tipos-usuario') + async findTiposUsuario() { + return await this.usuarioService.findAll(); + } + } diff --git a/src/usuario/usuario.service.ts b/src/usuario/usuario.service.ts index 0e7474e..e1fe796 100644 --- a/src/usuario/usuario.service.ts +++ b/src/usuario/usuario.service.ts @@ -361,6 +361,10 @@ export class UsuarioService { }; } + async findAll() { + return await this.tipoUsurioRepo.find() + } + /*c obtenerTodosLosUsuarios() { return await this.userRepo.find({ relations: ['tipoUsuario'],