From 3d04a42b626e46c142ec93099ac99768c1a13838 Mon Sep 17 00:00:00 2001 From: Giovanna Esmeralda Chavez Espino <79319097+gio-gigi@users.noreply.github.com> Date: Thu, 25 Apr 2024 13:18:35 -0600 Subject: [PATCH 1/2] correcciones para el registro de usuario --- backend/src/app.module.ts | 12 +++---- backend/src/auth-user/auth-user.controller.ts | 12 +++---- backend/src/auth-user/auth-user.module.ts | 3 +- backend/src/auth-user/auth-user.service.ts | 32 +++++++++++-------- backend/src/auth-user/dto/registrer.dto.ts | 1 - 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index c288b31..be6c245 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -7,11 +7,11 @@ import { UserModule } from './user/user.module'; import * as dotenv from 'dotenv'; import { Admin } from './admin/entities/admin.entity'; import { User } from './user/entities/user.entity'; -import { AuthModule } from './auth/auth.module'; -import { AuthService } from './auth/auth.service'; +//import { AuthModule } from './auth/auth.module'; +//import { AuthService } from './auth/auth.service'; import { JwtModule } from '@nestjs/jwt'; import { AuthUserModule } from './auth-user/auth-user.module'; -import { AuthAdminModule } from './auth-admin/auth-admin.module'; +//import { AuthAdminModule } from './auth-admin/auth-admin.module'; dotenv.config() @Module({ imports: [ @@ -27,12 +27,12 @@ dotenv.config() }), AdminModule, UserModule, - AuthModule, + //AuthModule, JwtModule, AuthUserModule, - AuthAdminModule, + //AuthAdminModule, ], controllers: [AppController], //rutas - providers: [AppService, AuthService], //funciones para comunicarse en la bd + providers: [AppService], //funciones para comunicarse en la bd }) export class AppModule {} diff --git a/backend/src/auth-user/auth-user.controller.ts b/backend/src/auth-user/auth-user.controller.ts index 824de47..34c6331 100644 --- a/backend/src/auth-user/auth-user.controller.ts +++ b/backend/src/auth-user/auth-user.controller.ts @@ -1,14 +1,14 @@ import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; import { AuthUserService } from './auth-user.service'; -import { CreateAuthUserDto } from './dto/registrer.dto'; -import { UpdateAuthUserDto } from './dto/login-user.dto'; +import { RegisterDto } from './dto/registrer.dto'; +import { LoginUserDto } from './dto/login-user.dto'; @Controller('auth-user') export class AuthUserController { constructor(private readonly authUserService: AuthUserService) {} @Post() - create(@Body() createAuthUserDto: CreateAuthUserDto) { + create(@Body() createAuthUserDto: RegisterDto) { return this.authUserService.create(createAuthUserDto); } @@ -22,13 +22,13 @@ export class AuthUserController { return this.authUserService.findOne(+id); } - @Patch(':id') - update(@Param('id') id: string, @Body() updateAuthUserDto: UpdateAuthUserDto) { + /**@Patch(':id') + update(@Param('id') id: string, @Body() updateAuthUserDto: LoginUserDto) { return this.authUserService.update(+id, updateAuthUserDto); } @Delete(':id') remove(@Param('id') id: string) { return this.authUserService.remove(+id); - } + }*/ } diff --git a/backend/src/auth-user/auth-user.module.ts b/backend/src/auth-user/auth-user.module.ts index f6884bb..fbb0b35 100644 --- a/backend/src/auth-user/auth-user.module.ts +++ b/backend/src/auth-user/auth-user.module.ts @@ -1,9 +1,10 @@ import { Module } from '@nestjs/common'; import { AuthUserService } from './auth-user.service'; import { AuthUserController } from './auth-user.controller'; +import { UserService } from 'src/user/user.service'; @Module({ controllers: [AuthUserController], - providers: [AuthUserService], + providers: [AuthUserService, UserService], }) export class AuthUserModule {} diff --git a/backend/src/auth-user/auth-user.service.ts b/backend/src/auth-user/auth-user.service.ts index 6edd747..dd3c9e8 100644 --- a/backend/src/auth-user/auth-user.service.ts +++ b/backend/src/auth-user/auth-user.service.ts @@ -2,7 +2,7 @@ import { Injectable, UnauthorizedException } from '@nestjs/common'; import { RegisterDto } from './dto/registrer.dto'; import { LoginUserDto } from './dto/login-user.dto'; import { UserService } from 'src/user/user.service'; -import { JwtService } from '@nestjs/jwt'; +//import { JwtService } from '@nestjs/jwt'; import { CreateUserDto } from 'src/user/dto/create-user.dto'; import * as bcryptjs from "bcryptjs"; @@ -11,6 +11,9 @@ export class AuthUserService { constructor(private readonly userService: UserService, ) {} + /* + Metodo para registrar un usuario + */ async register({ name, lastname, email, phone, password }: RegisterDto) { const hashedPassword = await bcryptjs.hash(password, 10); const userExists = await this.userService.findOne(email); @@ -26,22 +29,25 @@ export class AuthUserService { }); return user; } - - } - /* async login({ email, password }: LoginUserDto) { + /* + Metodo para loguear un usuario + */ + async login({ email, password }: LoginUserDto) { + + } + /* + Metodo para validar un usuario + */ + async validateUser(email: string, password: string) { const user = await this.userService.findOne(email); - if (!user) { - throw new Error('User not found'); - } - const isPasswordValid = await bcryptjs.compare(password, user.password); - if (!isPasswordValid) { - throw new Error('Invalid password'); - } - return user; - }*/ + + + } + create(createAuthUserDto: CreateUserDto) { + console.log(createAuthUserDto); return 'This action adds a new authUser'; } diff --git a/backend/src/auth-user/dto/registrer.dto.ts b/backend/src/auth-user/dto/registrer.dto.ts index 24bc86a..c00276f 100644 --- a/backend/src/auth-user/dto/registrer.dto.ts +++ b/backend/src/auth-user/dto/registrer.dto.ts @@ -22,5 +22,4 @@ export class RegisterDto { @Transform(({ value }) => value.trim()) password: string; - } -- GitLab From db50fe1e75b3a6e02710f33ca3480bf5c6531ee1 Mon Sep 17 00:00:00 2001 From: Giovanna Esmeralda Chavez Espino <79319097+gio-gigi@users.noreply.github.com> Date: Thu, 25 Apr 2024 17:42:55 -0600 Subject: [PATCH 2/2] finalizacion de login/ register para usuario --- backend/src/app.module.ts | 6 +-- backend/src/auth-user/auth-user.controller.ts | 31 ++++-------- backend/src/auth-user/auth-user.service.ts | 48 +++++++------------ 3 files changed, 27 insertions(+), 58 deletions(-) diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index be6c245..636933b 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -7,11 +7,9 @@ import { UserModule } from './user/user.module'; import * as dotenv from 'dotenv'; import { Admin } from './admin/entities/admin.entity'; import { User } from './user/entities/user.entity'; -//import { AuthModule } from './auth/auth.module'; -//import { AuthService } from './auth/auth.service'; import { JwtModule } from '@nestjs/jwt'; import { AuthUserModule } from './auth-user/auth-user.module'; -//import { AuthAdminModule } from './auth-admin/auth-admin.module'; + dotenv.config() @Module({ imports: [ @@ -27,10 +25,8 @@ dotenv.config() }), AdminModule, UserModule, - //AuthModule, JwtModule, AuthUserModule, - //AuthAdminModule, ], controllers: [AppController], //rutas providers: [AppService], //funciones para comunicarse en la bd diff --git a/backend/src/auth-user/auth-user.controller.ts b/backend/src/auth-user/auth-user.controller.ts index 34c6331..02b71d9 100644 --- a/backend/src/auth-user/auth-user.controller.ts +++ b/backend/src/auth-user/auth-user.controller.ts @@ -1,34 +1,21 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { Controller, Get, Post, Body, Patch, Param, Delete, HttpStatus, HttpCode } from '@nestjs/common'; import { AuthUserService } from './auth-user.service'; import { RegisterDto } from './dto/registrer.dto'; import { LoginUserDto } from './dto/login-user.dto'; + @Controller('auth-user') export class AuthUserController { constructor(private readonly authUserService: AuthUserService) {} - @Post() - create(@Body() createAuthUserDto: RegisterDto) { - return this.authUserService.create(createAuthUserDto); - } - - @Get() - findAll() { - return this.authUserService.findAll(); + @Post('register') + register(@Body() registerUser: RegisterDto) { + return this.authUserService.register(registerUser); } - @Get(':id') - findOne(@Param('id') id: string) { - return this.authUserService.findOne(+id); + @HttpCode(HttpStatus.OK) + @Post('login') + login(@Body() loginUser: LoginUserDto){ + return this.authUserService.login(loginUser); } - - /**@Patch(':id') - update(@Param('id') id: string, @Body() updateAuthUserDto: LoginUserDto) { - return this.authUserService.update(+id, updateAuthUserDto); - } - - @Delete(':id') - remove(@Param('id') id: string) { - return this.authUserService.remove(+id); - }*/ } diff --git a/backend/src/auth-user/auth-user.service.ts b/backend/src/auth-user/auth-user.service.ts index dd3c9e8..af30a90 100644 --- a/backend/src/auth-user/auth-user.service.ts +++ b/backend/src/auth-user/auth-user.service.ts @@ -3,7 +3,6 @@ import { RegisterDto } from './dto/registrer.dto'; import { LoginUserDto } from './dto/login-user.dto'; import { UserService } from 'src/user/user.service'; //import { JwtService } from '@nestjs/jwt'; -import { CreateUserDto } from 'src/user/dto/create-user.dto'; import * as bcryptjs from "bcryptjs"; @Injectable() @@ -16,7 +15,7 @@ export class AuthUserService { */ async register({ name, lastname, email, phone, password }: RegisterDto) { const hashedPassword = await bcryptjs.hash(password, 10); - const userExists = await this.userService.findOne(email); + const userExists = await this.userService.findOneByEmail(email); if (userExists) { throw new UnauthorizedException('User already exists'); }else{ @@ -27,7 +26,10 @@ export class AuthUserService { phone, password: hashedPassword, }); - return user; + return { + message: 'User created', + user, + }; } } @@ -35,35 +37,19 @@ export class AuthUserService { Metodo para loguear un usuario */ async login({ email, password }: LoginUserDto) { + const user = await this.userService.findOneByEmail(email); + if (!user) { + throw new UnauthorizedException('Invalid email'); + } + const passwordValida = await bcryptjs.compare(password, user.password); + if (!passwordValida) { + throw new UnauthorizedException('Invalid password'); + } + return { + message: 'User logged', + user, + }; } - /* - Metodo para validar un usuario - */ - async validateUser(email: string, password: string) { - const user = await this.userService.findOne(email); - - - } - - create(createAuthUserDto: CreateUserDto) { - console.log(createAuthUserDto); - return 'This action adds a new authUser'; - } - - findAll() { - return `This action returns all authUser`; - } - - findOne(id: number) { - return `This action returns a #${id} authUser`; - } - - update(id: number, registrerUser: RegisterDto) { - return `This action updates a #${id} authUser`; - } - remove(id: number) { - return `This action removes a #${id} authUser`; - } } -- GitLab