diff --git a/src/main.ts b/src/main.ts index 21b52a4..be77c31 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,9 +10,9 @@ async function bootstrap() { app.useGlobalPipes(new ValidationPipe()); app.enableCors( - // { - // origin: configService.get('Front_URL'), - // } + { + origin: configService.get('Front_URL'), + } ); await app.listen(process.env.PORT ?? 3000); diff --git a/src/servicio/servicio.controller.ts b/src/servicio/servicio.controller.ts index b1a964a..3bed5e5 100644 --- a/src/servicio/servicio.controller.ts +++ b/src/servicio/servicio.controller.ts @@ -32,3 +32,4 @@ export class ServicioController { return this.servicioService.remove(+id); } } +//IO \ No newline at end of file diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index c501e83..8ea80b9 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -1,4 +1,5 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Request } from '@nestjs/common'; +import { Controller, Get, Post, Body, UseGuards, Request, Res } from '@nestjs/common'; +import type { Response } from 'express'; import { UserService } from './user.service'; import { CreateUserDto } from './dto/create-user.dto'; import { AuthGuard } from '@nestjs/passport'; @@ -7,10 +8,11 @@ import { AuthGuard } from '@nestjs/passport'; export class UserController { constructor(private readonly userService: UserService) { } - @Post() - Login(@Body() user: CreateUserDto) { - return this.userService.Login(user); - } +@Post() +async Login(@Body() data: CreateUserDto, @Res() res: Response) { + return this.userService.Login(data, res); +} + @UseGuards(AuthGuard('jwt')) @Get('/validate-token') @@ -19,3 +21,4 @@ export class UserController { } } +//IO \ No newline at end of file diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 0743c55..8bd2a63 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -1,4 +1,5 @@ import { Injectable, NotFoundException } from '@nestjs/common'; +import { Response } from 'express'; import { CreateUserDto } from './dto/create-user.dto'; import { InjectRepository } from '@nestjs/typeorm'; import { User } from './entities/user.entity'; @@ -24,20 +25,27 @@ export class UserService { if (!user) { throw new NotFoundException( - `El usuario ${usuario} no fue encontrado`, + `El usuario o la contraseña es incorrecta`, ); } return user; } - async Login(data: CreateUserDto): Promise<{ access_token: string }> { + async Login(data: CreateUserDto, res: Response) { const user = await this.findOneByNameandPassword(data); const payload = { id: user.id_usuario, usuario: user.usuario }; - return { - access_token: await this.jwtService.signAsync(payload), - }; + const token = await this.jwtService.signAsync(payload); + + res.cookie('token', token, { + httpOnly: true, + secure: process.env.NODE_ENV === 'production', // en dev desactívalo + sameSite: 'strict', + path: '/', + }); + + return res.json({ message: 'Inicio de sesión exitoso' }); } }