From 28aba967b175a7bbf4a47bd0edc9c86313d75172 Mon Sep 17 00:00:00 2001 From: IO Date: Sat, 27 Jul 2024 21:03:38 -0600 Subject: [PATCH] fixed update user --- src/app.service.ts | 2 +- src/auth/auth.service.ts | 17 +++++++++++------ src/permissions/roles.guard.ts | 9 ++++++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/app.service.ts b/src/app.service.ts index 548dd8c..f6d00f9 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -3,7 +3,7 @@ import { Injectable } from '@nestjs/common'; @Injectable() export class AppService { getHello(): string { - console.log("it's ok ") + console.log("it's ok IO") return "hei verden"; } diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index fe1d45c..ab39039 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -73,13 +73,18 @@ export class AuthService { } //update user - async update(id_usuario, data:registerDto) { + async update(id_usuario: number, data: registerDto) { + const { contraseña } = data; - const{contraseña}=data; - - if(contraseña){ + if (contraseña) { const hashedpassword = await hash(contraseña, 10); - data = {...data,contraseña:hashedpassword}; + data = { ...data, contraseña: hashedpassword }; + } else { + const user = await this.userRepository.findOne({ where: { id_usuario } }); + if (!user) { + throw new HttpException('User not found', 404); + } + data = { ...data, contraseña: user.contraseña }; } return await this.userRepository.update(id_usuario, data); @@ -108,7 +113,7 @@ export class AuthService { throw new UnauthorizedException('Token has expired'); } - return user; + return ; } catch (error) { throw new UnauthorizedException('validation token failed'); } diff --git a/src/permissions/roles.guard.ts b/src/permissions/roles.guard.ts index aa766ec..9671a16 100644 --- a/src/permissions/roles.guard.ts +++ b/src/permissions/roles.guard.ts @@ -44,11 +44,14 @@ export class RolesGuard implements CanActivate { ); const userRole: Role = userPayload.id_tipo_usuario; - if (userRole === undefined) { - return false; // Si el token no contiene roles, se niega el acceso + const userId: number = userPayload.id_usuario; + + const paramId = parseInt(request.params.id_usuario, 10); + if (requiredRoles.includes(userRole) || userId === paramId) { + return true; } - return requiredRoles.includes(userRole); + return false; } catch (error) { throw new UnauthorizedException('Invalid token'); }