diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index c288b3193701991c95381a073b5dff10d3f6660b..636933b28ed8c7739c53edc434ca2139a9ed41b2 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,12 +25,10 @@ dotenv.config() }), AdminModule, UserModule, - AuthModule, JwtModule, AuthUserModule, - 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 824de4748c9027072a2373f68eaaa230eddf06ee..02b71d97d1f6697a6e41b352b426e4818056fb29 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 { 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) { - return this.authUserService.create(createAuthUserDto); - } - - @Get() - findAll() { - return this.authUserService.findAll(); - } - - @Get(':id') - findOne(@Param('id') id: string) { - return this.authUserService.findOne(+id); - } - - @Patch(':id') - update(@Param('id') id: string, @Body() updateAuthUserDto: UpdateAuthUserDto) { - return this.authUserService.update(+id, updateAuthUserDto); + @Post('register') + register(@Body() registerUser: RegisterDto) { + return this.authUserService.register(registerUser); } - @Delete(':id') - remove(@Param('id') id: string) { - return this.authUserService.remove(+id); + @HttpCode(HttpStatus.OK) + @Post('login') + login(@Body() loginUser: LoginUserDto){ + return this.authUserService.login(loginUser); } } diff --git a/backend/src/auth-user/auth-user.module.ts b/backend/src/auth-user/auth-user.module.ts index f6884bb368050558d368b1a741fc9894c6e31d85..fbb0b3522ac03fd130eb4c6fff65fc9a30c90950 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 6edd747ee45f73bc63a5138d36c8f919445eff20..af30a904519956b3eac308d1eae04953753c931c 100644 --- a/backend/src/auth-user/auth-user.service.ts +++ b/backend/src/auth-user/auth-user.service.ts @@ -2,8 +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 { CreateUserDto } from 'src/user/dto/create-user.dto'; +//import { JwtService } from '@nestjs/jwt'; import * as bcryptjs from "bcryptjs"; @Injectable() @@ -11,9 +10,12 @@ 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); + const userExists = await this.userService.findOneByEmail(email); if (userExists) { throw new UnauthorizedException('User already exists'); }else{ @@ -24,40 +26,30 @@ export class AuthUserService { phone, password: hashedPassword, }); - return user; + return { + message: 'User created', + user, + }; } - - } - /* async login({ email, password }: LoginUserDto) { - const user = await this.userService.findOne(email); + /* + Metodo para loguear un usuario + */ + async login({ email, password }: LoginUserDto) { + const user = await this.userService.findOneByEmail(email); if (!user) { - throw new Error('User not found'); + throw new UnauthorizedException('Invalid email'); } - const isPasswordValid = await bcryptjs.compare(password, user.password); - if (!isPasswordValid) { - throw new Error('Invalid password'); + const passwordValida = await bcryptjs.compare(password, user.password); + if (!passwordValida) { + throw new UnauthorizedException('Invalid password'); } - return user; - }*/ - create(createAuthUserDto: CreateUserDto) { - 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`; + return { + message: 'User logged', + user, + }; + } - remove(id: number) { - return `This action removes a #${id} authUser`; - } } diff --git a/backend/src/auth-user/dto/registrer.dto.ts b/backend/src/auth-user/dto/registrer.dto.ts index 24bc86a4a557b2a63ec7dbf5e4e3aa7db5acf94f..c00276f814095c8c1d60219cb4842d61d1192106 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; - }