From ab995029bab642b7eddfb4653b59ded28721e36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:32:20 -0600 Subject: [PATCH 1/5] renombrando atributo a userId --- backend/src/user/entities/user.entity.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/user/entities/user.entity.ts b/backend/src/user/entities/user.entity.ts index 96037386..01e24044 100644 --- a/backend/src/user/entities/user.entity.ts +++ b/backend/src/user/entities/user.entity.ts @@ -3,7 +3,7 @@ import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() - id: number; + userId: number; @Column({ unique: true, -- GitLab From 1be773361906a3bf59aff7cbdb0d56d3680027f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:33:14 -0600 Subject: [PATCH 2/5] cambiando metodo de findOneByOrFail a findOneBy --- backend/src/user/user.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/user/user.service.ts b/backend/src/user/user.service.ts index 82ef1863..c538fcd5 100644 --- a/backend/src/user/user.service.ts +++ b/backend/src/user/user.service.ts @@ -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) { -- GitLab From e65589aa8ca7113d709c240e639a8600fdab37dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:33:38 -0600 Subject: [PATCH 3/5] renombrando atributo de dto --- backend/src/auth/user/dto/user-signin-res.dto.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/src/auth/user/dto/user-signin-res.dto.ts b/backend/src/auth/user/dto/user-signin-res.dto.ts index 4491c55b..f703702a 100644 --- a/backend/src/auth/user/dto/user-signin-res.dto.ts +++ b/backend/src/auth/user/dto/user-signin-res.dto.ts @@ -1,6 +1,8 @@ import { ApiProperty } from '@nestjs/swagger'; export class UserSigninResDto { + @ApiProperty() + userId: number; @ApiProperty() email: string; @ApiProperty() -- GitLab From d1764a47572e94b59d815201464eb2a6a60a70b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:35:11 -0600 Subject: [PATCH 4/5] corrigiendo error de login --- backend/src/auth/user/authUserservice.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/backend/src/auth/user/authUserservice.ts b/backend/src/auth/user/authUserservice.ts index 2a0011bf..7a2eef25 100644 --- a/backend/src/auth/user/authUserservice.ts +++ b/backend/src/auth/user/authUserservice.ts @@ -1,8 +1,7 @@ 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 { + async signUp(createAdminDto: CreateUserDto): Promise { 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 { 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, -- GitLab From 1646205cd1c8ad47de3dfd1b19bec65f085ced8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:35:49 -0600 Subject: [PATCH 5/5] agregando docs para todos los endpoints --- backend/src/auth/user/authUsercontroller.ts | 22 ++++++++++----------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/backend/src/auth/user/authUsercontroller.ts b/backend/src/auth/user/authUsercontroller.ts index dd4bd9aa..bc072bbf 100644 --- a/backend/src/auth/user/authUsercontroller.ts +++ b/backend/src/auth/user/authUsercontroller.ts @@ -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; } -- GitLab