pcpuma_unam_api/src/auth/auth.service.ts

78 lines
2.5 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-22 02:52:40 +00:00
import { JwtPayload } from './dto/jwt-payload';
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-05-02 19:40:17 +00:00
return this.operadorService.findAdmin(admin, false).then((operador) => {
2022-08-31 16:29:48 +00:00
// if (!operador.activo)
// throw new ConflictException('Esta cuenta se encuentra desactivada.');
2022-04-22 03:34:10 +00:00
const payload: JwtPayload = {
id_operador: operador.id_operador,
2022-08-11 21:38:56 +00:00
nombre: operador.nombre,
operador: operador.operador,
institucion: operador.institucion
2022-08-12 00:04:41 +00:00
? { id_institucion: operador.institucion.id_institucion }
2022-08-11 21:38:56 +00:00
: null,
tipoUsuario: {
id_tipo_usuario: operador.tipoUsuario.id_tipo_usuario,
},
2022-04-22 03:34:10 +00:00
};
return { operador, token: this.jwtService.sign(payload) };
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-04-22 02:52:40 +00:00
const modulo = await this.moduloService.findById(id_modulo);
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.',
);
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-08-31 00:36:53 +00:00
return this.usuarioService.informacionUsuario(usuario).then((data) => {
if (
!data.usuario ||
!this.bcryptService.comparar(password, data.password)
)
2022-08-31 16:29:48 +00:00
throw new BadRequestException(
2022-08-31 00:36:53 +00:00
'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
}