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

corrigiendo error de login

parent e65589aa
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
import {
  BadRequestException,
  HttpException,
  HttpStatus,
  Injectable,
  UnauthorizedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { EncryptionService } from '../encryption/encryption.service';
@@ -21,7 +20,7 @@ export class AuthUserService {
    private encryptionService: EncryptionService,
  ) {}

  async signUp(createAdminDto: CreateUserDto): Promise<string> {
  async signUp(createAdminDto: CreateUserDto): Promise<UserSigninResDto> {
    const loginAdminDto: LoginUserDto = {
      email: createAdminDto.email,
      password: createAdminDto.password,
@@ -30,30 +29,32 @@ export class AuthUserService {
      createAdminDto.password,
    );
    createAdminDto.password = hashedPwd;
    if (this.userService.userExists(createAdminDto.email)) {
    if (await this.userService.userExists(createAdminDto.email)) {
      throw new BadRequestException('User already exists');
    }
    await this.userService.create(createAdminDto);

    const adminSigninResDto: UserSigninResDto =
      await this.signIn(loginAdminDto);
    return adminSigninResDto.token;
    return adminSigninResDto;
  }

  async signIn(logInAdmin: LoginUserDto): Promise<UserSigninResDto> {
    const user: User = await this.userService.findOne(logInAdmin.email);
    if (!user) throw new UnauthorizedException('Invalid credentials');
    const validPwd: boolean = await this.encryptionService.comparePassword(
      logInAdmin.password,
      user.password,
    );
    if (!validPwd) {
      throw new HttpException('Invalid credentials', HttpStatus.UNAUTHORIZED);
      throw new UnauthorizedException('Invalid credentials');
    }
    const accessToken = await this.jwtService.sign(
      { email: user.email, name: user.name, lastName: user.lastName },
      { secret: JwtConstants.SECRET },
    );
    const userSigninResDto: UserSigninResDto = {
      userId: user.userId,
      email: user.email,
      name: user.name,
      lastName: user.lastName,