Commit 8af92d71 authored by Diego Iván's avatar Diego Iván
Browse files

agregando endpoint para cambiar contraseña para un usuario

parent d2d87795
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -14,7 +14,10 @@ export class AuthUserGuard implements CanActivate {
    if (!authorization) throw new UnauthorizedException('session expired! Please sign In');
    authorization = authorization.split(' ')[1];
    const jwtPayload = await this.authUserService.validateToken(authorization);
    if (!jwtPayload) throw new UnauthorizedException('session expired! Please sign In');

    const user = await this.userService.findOne(jwtPayload.email);
    if (!user) throw new UnauthorizedException('session expired! Please sign In');
    request.user = { ...user };
    return true;
  }
+3 −3
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import { LoginUserDto } from './dto/login-user.dto';
import { UserSigninResDto } from './dto/user-signin-res.dto';
import { AuthUserGuard } from './authUser.guard';
import { CustomUserRequest } from './interface/customUserReq';
import { UpdatePwdDto } from './dto/update-pwd.dto';

@Controller('')
@ApiTags('Create user account and sign in as user')
@@ -44,8 +45,7 @@ export class AuthUserController {
  @UseGuards(AuthUserGuard)
  @ApiBearerAuth('jwt')
  @Patch('user/change-password')
  async changePassword(@Req() req: CustomUserRequest) {
    console.log({ user: req.user });
    return true;
  async changePassword(@Req() req: CustomUserRequest, @Body() updatePwdDto: UpdatePwdDto) {
    return this.authUserService.changePassword(req.user.email, updatePwdDto);
  }
}
+12 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import { CreateUserDto } from 'src/user/dto/create-user.dto';
import { LoginUserDto } from './dto/login-user.dto';
import { ALL_ROLES } from 'src/shared/enum/admin-role.enum';
import { PayloadJwtDto } from 'src/shared/dto/payload-jwt.dto';
import { UpdatePwdDto } from './dto/update-pwd.dto';

@Injectable()
export class AuthUserService {
@@ -64,4 +65,15 @@ export class AuthUserService {
      throw new UnauthorizedException('Invalid token');
    }
  }

  async changePassword(email: string, updatePwdDto: UpdatePwdDto) {
    const user: User = await this.userService.findOne(email);
    const prevPwdHashed = user.password;
    const validPwd: boolean = await this.encryptionService.comparePassword(updatePwdDto.prevPassword, prevPwdHashed);

    if (!validPwd) throw new UnauthorizedException('Invalid password');

    const newPwdHashed = await this.encryptionService.hashPassword(updatePwdDto.newPassword);
    await this.userService.updatePassword(email, newPwdHashed);
  }
}
+4 −0
Original line number Diff line number Diff line
@@ -24,4 +24,8 @@ export class UserService {
    if (user) return true;
    else return false;
  }

  async updatePassword(email: string, password: string) {
    await this.userRepository.update({ email }, { password });
  }
}