Commit 9eecacc6 authored by Lorenzo Trujillo Rojas's avatar Lorenzo Trujillo Rojas
Browse files

Merge branch 'main' into 'main'

Agregando docs y corrigiendo bug en login de usuarios

See merge request ltrpro/pueblosmagicosconia!15
parents dcc04c83 72ed1e73
Loading
Loading
Loading
Loading
+10 −12
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import {
import { AuthUserService } from './authUserservice';
import { CreateUserDto } from 'src/user/dto/create-user.dto';
import { LoginUserDto } from './dto/login-user.dto';
import { UserSigninResDto } from './dto/user-signin-res.dto';

@Controller('')
@ApiTags('Create user account and sign in as user')
@@ -18,31 +19,28 @@ export class AuthUserController {
  @ApiBody({ type: CreateUserDto })
  @ApiBearerAuth()
  @ApiCreatedResponse({
    content: {
      'application/json': {
        example: {
          token: 'token',
        },
      },
    },
    type: UserSigninResDto,
  })
  @Post('user/signup')
  async signUp(@Body() createAdminDto: CreateUserDto) {
    try {
      const accessToken = await this.authUserService.signUp(createAdminDto);
      return { token: accessToken };
      const adminSigninResDto =
        await this.authUserService.signUp(createAdminDto);
      return { user: adminSigninResDto };
    } catch (e) {
      throw e;
    }
  }
  @ApiBody({ type: LoginUserDto })
  @ApiCreatedResponse({
    type: UserSigninResDto,
  })
  @ApiUnauthorizedResponse()
  @Post('user/signin')
  async signIn(@Body() loginAdminDto: LoginUserDto) {
    try {
      const adminSigninResDto =
        await this.authUserService.signIn(loginAdminDto);
      return adminSigninResDto;
      const userSigninResDto = await this.authUserService.signIn(loginAdminDto);
      return { user: userSigninResDto };
    } catch (e) {
      throw e;
    }
+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,
+2 −0
Original line number Diff line number Diff line
import { ApiProperty } from '@nestjs/swagger';

export class UserSigninResDto {
  @ApiProperty()
  userId: number;
  @ApiProperty()
  email: string;
  @ApiProperty()
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;
  userId: number;

  @Column({
    unique: true,
+1 −1
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ export class UserService {
  ) {}

  async findOne(email: string) {
    return await this.userRepository.findOneByOrFail({ email });
    return await this.userRepository.findOneBy({ email });
  }

  async create(createUserDto: CreateUserDto) {