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] 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