funciona rememberMe
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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"},
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user