funciones partidas
This commit is contained in:
parent
e397bfb6cc
commit
85b9709ecb
@ -5,6 +5,7 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { JwtService } from '@nestjs/jwt';
|
import { JwtService } from '@nestjs/jwt';
|
||||||
import { Operador } from '../operador/entity/operador.entity';
|
import { Operador } from '../operador/entity/operador.entity';
|
||||||
|
import { Usuario } from '../usuario/entity/usuario.entity';
|
||||||
import { BcryptService } from '../bcrypt/bcrypt.service';
|
import { BcryptService } from '../bcrypt/bcrypt.service';
|
||||||
import { ModuloService } from '../modulo/modulo.service';
|
import { ModuloService } from '../modulo/modulo.service';
|
||||||
import { OperadorService } from '../operador/operador.service';
|
import { OperadorService } from '../operador/operador.service';
|
||||||
@ -26,20 +27,18 @@ export class AuthService {
|
|||||||
loginAdmin(admin: string, password: string) {
|
loginAdmin(admin: string, password: string) {
|
||||||
return this.operadorService.findInfoOperadorAdmin(admin).then((admin) => {
|
return this.operadorService.findInfoOperadorAdmin(admin).then((admin) => {
|
||||||
this.validacionBasica(admin, password);
|
this.validacionBasica(admin, password);
|
||||||
|
this.validacionOperador(admin);
|
||||||
|
|
||||||
const JwtPayload: JwtPayloadAdmin = {
|
const JwtPayload: JwtPayloadAdmin = {
|
||||||
Operador: {
|
Operador: {
|
||||||
id_operador: admin.id_operador,
|
id_operador: admin.id_operador,
|
||||||
operador: admin.operador,
|
operador: admin.operador,
|
||||||
nombre: admin.nombre,
|
nombre: admin.nombre,
|
||||||
institucion: {
|
institucion: { id_institucion: admin.institucion.id_institucion },
|
||||||
id_institucion: admin.institucion.id_institucion,
|
tipoUsuario: { id_tipo_usuario: admin.tipoUsuario.id_tipo_usuario },
|
||||||
},
|
|
||||||
tipoUsuario: {
|
|
||||||
id_tipo_usuario: admin.tipoUsuario.id_tipo_usuario,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return { token: this.jwtService.sign(JwtPayload) };
|
return { token: this.jwtService.sign(JwtPayload) };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -51,6 +50,7 @@ export class AuthService {
|
|||||||
.findInfoOperador(modulo.institucion, operador)
|
.findInfoOperador(modulo.institucion, operador)
|
||||||
.then((operador) => {
|
.then((operador) => {
|
||||||
this.validacionBasica(operador, password);
|
this.validacionBasica(operador, password);
|
||||||
|
this.validacionOperador(operador);
|
||||||
|
|
||||||
const JwtPayload: JwtPayloadOperador = {
|
const JwtPayload: JwtPayloadOperador = {
|
||||||
Operador: {
|
Operador: {
|
||||||
@ -78,13 +78,7 @@ export class AuthService {
|
|||||||
.then((usuario) => {
|
.then((usuario) => {
|
||||||
if (usuario && !usuario.password)
|
if (usuario && !usuario.password)
|
||||||
throw new BadRequestException('Este usuario no ha sido registrado.');
|
throw new BadRequestException('Este usuario no ha sido registrado.');
|
||||||
if (
|
this.validacionBasica(usuario, password);
|
||||||
!usuario ||
|
|
||||||
!this.bcryptService.comparar(password, usuario.password)
|
|
||||||
)
|
|
||||||
throw new BadRequestException(
|
|
||||||
'Usuario y/o password incorrectos, ingresa unas credenciales válidas.',
|
|
||||||
);
|
|
||||||
|
|
||||||
const JwtPayload: JwtPayloadUsuario = {
|
const JwtPayload: JwtPayloadUsuario = {
|
||||||
Usuario: {
|
Usuario: {
|
||||||
@ -101,11 +95,22 @@ export class AuthService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private validacionBasica(operador: Operador, password: string) {
|
private validacionBasica(
|
||||||
if (!operador || !this.bcryptService.comparar(password, operador.password))
|
operadorUsuario: Operador | Usuario,
|
||||||
|
password: string,
|
||||||
|
) {
|
||||||
|
// Valido que el operador o usuario existan y que el password sea el correcto
|
||||||
|
if (
|
||||||
|
!operadorUsuario ||
|
||||||
|
!this.bcryptService.comparar(password, operadorUsuario.password)
|
||||||
|
)
|
||||||
throw new BadRequestException(
|
throw new BadRequestException(
|
||||||
'Usuario y/o password incorrectos, ingresa unas credenciales válidas.',
|
'Usuario y/o password incorrectos, ingresa unas credenciales válidas.',
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private validacionOperador(operador: Operador) {
|
||||||
|
// Valido que el operador este activo
|
||||||
if (!operador.activo)
|
if (!operador.activo)
|
||||||
throw new UnauthorizedException('Esta cuenta se encuentra desactivada.');
|
throw new UnauthorizedException('Esta cuenta se encuentra desactivada.');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user