pcpuma_unam_api/src/auth/auth.service.ts

79 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-04-22 02:52:40 +00:00
import {
2022-08-31 16:29:48 +00:00
BadRequestException,
2022-04-22 02:52:40 +00:00
Injectable,
UnauthorizedException,
} from '@nestjs/common';
2022-04-21 21:31:26 +00:00
import { JwtService } from '@nestjs/jwt';
2022-04-22 01:33:05 +00:00
import { BcryptService } from '../bcrypt/bcrypt.service';
2022-04-22 02:52:40 +00:00
import { ModuloService } from '../modulo/modulo.service';
2022-04-22 01:33:05 +00:00
import { OperadorService } from '../operador/operador.service';
import { UsuarioService } from '../usuario/usuario.service';
2022-04-21 21:31:26 +00:00
@Injectable()
export class AuthService {
2022-04-22 01:33:05 +00:00
constructor(
private bcryptService: BcryptService,
2022-04-22 02:52:40 +00:00
private jwtService: JwtService,
private moduloService: ModuloService,
2022-04-22 01:33:05 +00:00
private operadorService: OperadorService,
private usuarioService: UsuarioService,
) {}
2022-04-21 21:31:26 +00:00
loginAdmin(admin: string, password: string) {
2022-08-31 16:52:44 +00:00
return this.operadorService.informacionAdmin(admin).then((data) => {
if (
!data.operador ||
!this.bcryptService.comparar(password, data.password)
)
throw new BadRequestException(
'Usuario y/o password incorrectos, ingresa unas credenciales válidas.',
);
if (!data.activo)
throw new UnauthorizedException(
'Esta cuenta se encuentra desactivada.',
);
return { token: this.jwtService.sign(data.operador) };
2022-04-22 01:33:05 +00:00
});
}
2022-06-13 10:33:53 +00:00
async loginOperador(id_modulo: number, operador: string, password: string) {
2022-10-05 01:46:31 +00:00
const modulo = await this.moduloService.findInfoModuloById(id_modulo);
2022-04-22 02:52:40 +00:00
return this.operadorService
2022-08-31 16:29:48 +00:00
.informacionOperador(modulo.institucion, operador)
.then((data) => {
2022-07-25 03:35:00 +00:00
if (
2022-08-31 16:29:48 +00:00
!data.operador ||
!this.bcryptService.comparar(password, data.password)
2022-07-25 03:35:00 +00:00
)
2022-08-31 16:29:48 +00:00
throw new BadRequestException(
'Usuario y/o password incorrectos, ingresa unas credenciales válidas.',
2022-07-25 03:35:00 +00:00
);
2022-08-31 16:29:48 +00:00
if (!data.activo)
throw new UnauthorizedException(
'Esta cuenta se encuentra desactivada.',
);
2022-09-20 17:54:04 +00:00
if (data.operador.tipoUsuario.id_tipo_usuario === 4)
data.operador.id_modulo = modulo.id_modulo;
2022-08-31 16:29:48 +00:00
return { token: this.jwtService.sign(data.operador) };
2022-04-22 02:52:40 +00:00
});
2022-04-22 01:33:05 +00:00
}
loginUsuario(usuario: string, password: string) {
2022-09-01 04:43:55 +00:00
return this.usuarioService
.informacionUsuarioByUsuario(usuario)
.then((data) => {
if (!data.password)
throw new BadRequestException('Este usuario no ha sido registrado.');
if (
!data.usuario ||
!this.bcryptService.comparar(password, data.password)
)
throw new BadRequestException(
'Usuario y/o password incorrectos, ingresa unas credenciales válidas.',
);
return { token: this.jwtService.sign(data.usuario) };
});
}
2022-04-21 21:31:26 +00:00
}