diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index e670fe4..d82dd07 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -29,7 +29,8 @@ export class AuthController { @HttpCode(HttpStatus.OK) @Post('login') async signIn(@Body() data: registerDto) { - return this.authService.signIn(data); + const { rememberMe } = data; + return this.authService.signIn(data, rememberMe); } //@UseGuards() diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index 4e64999..a6834e9 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -1,4 +1,3 @@ - import { Module } from '@nestjs/common'; import { AuthService } from './auth.service'; import { UsersModule } from '../users/users.module'; @@ -17,7 +16,7 @@ import { User } from 'src/users/entities/user.entity'; return{ global: true, secret: configService.get('JWT_SECRET'), - signOptions: { expiresIn: "1h"}, + signOptions: { expiresIn: "7h"}, } } }), diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index fbe24e9..fe1d45c 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -38,7 +38,7 @@ export class AuthService { } //singin - async signIn(data: registerDto) { + async signIn(data: registerDto, rememberMe: boolean) { const { usuario, contraseƱa } = data; const user = await this.usersService.findOne(usuario); if (!user) { @@ -55,16 +55,21 @@ export class AuthService { usuario: user.usuario, id_tipo_usuario: user.id_tipo_usuario, }; - const token = this.jwtService.sign(payload); + + const token = this.jwtService.sign(payload, { + expiresIn: rememberMe ? '31d' : '7h', + }); + + const expirationTime = rememberMe ? 31 * 24 * 3600 * 1000 : 7 * 3600 * 1000; await this.usersService.updateTokenAndDates( user.id_usuario, token, new Date(), - new Date(Date.now() + 3600 * 1000), + new Date(Date.now() + expirationTime), ); - return { token: token }; + return { token }; } //update user diff --git a/src/auth/dto/registerDto.dto.ts b/src/auth/dto/registerDto.dto.ts index f544da4..1091baa 100644 --- a/src/auth/dto/registerDto.dto.ts +++ b/src/auth/dto/registerDto.dto.ts @@ -1,4 +1,4 @@ -import {IsString, IsNotEmpty,IsNumber } from "class-validator"; +import {IsString, IsNotEmpty,IsNumber, IsBoolean, IsOptional } from "class-validator"; export class registerDto{ @@ -22,4 +22,8 @@ export class registerDto{ @IsNotEmpty() correo: string; + @IsBoolean() + @IsOptional() + rememberMe: boolean; + }