Files
pcpuma_unam_api/src/auth/auth.service.ts
T

79 lines
2.6 KiB
TypeScript
Raw Normal View History

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