bcrypt como servicio y login operador listo
This commit is contained in:
parent
ede4bbd0dc
commit
e5610f0dd3
@ -46,6 +46,7 @@ import { TipoEntrada } from './tipo-entrada/entity/tipo-entrada.entity';
|
||||
import { TipoUsuario } from './tipo-usuario/entity/tipo-usuario.entity';
|
||||
import { Usuario } from './usuario/entity/usuario.entity';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { BcryptModule } from './bcrypt/bcrypt.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -59,7 +60,7 @@ import { AuthModule } from './auth/auth.module';
|
||||
database: configService.get<string>('DB'),
|
||||
username: configService.get<string>('DB_USER'),
|
||||
password: configService.get<string>('DB_PASSWORD'),
|
||||
port: configService.get<number>('DB_PORT'),
|
||||
port: Number(configService.get<string>('DB_PORT')),
|
||||
synchronize: true,
|
||||
entities: [
|
||||
Carrera,
|
||||
@ -109,6 +110,7 @@ import { AuthModule } from './auth/auth.module';
|
||||
TipoUsuarioModule,
|
||||
UsuarioModule,
|
||||
InstitucionTipoCarritoModule,
|
||||
BcryptModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
8
src/bcrypt/bcrypt.module.ts
Normal file
8
src/bcrypt/bcrypt.module.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { BcryptService } from './bcrypt.service';
|
||||
|
||||
@Module({
|
||||
providers: [BcryptService],
|
||||
exports: [BcryptService],
|
||||
})
|
||||
export class BcryptModule {}
|
18
src/bcrypt/bcrypt.service.spec.ts
Normal file
18
src/bcrypt/bcrypt.service.spec.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { BcryptService } from './bcrypt.service';
|
||||
|
||||
describe('BcryptService', () => {
|
||||
let service: BcryptService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [BcryptService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<BcryptService>(BcryptService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
20
src/bcrypt/bcrypt.service.ts
Normal file
20
src/bcrypt/bcrypt.service.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
|
||||
@Injectable()
|
||||
export class BcryptService {
|
||||
constructor(private configService: ConfigService) {}
|
||||
|
||||
encriptar(password: string) {
|
||||
const salt = bcrypt.genSaltSync(
|
||||
Number(this.configService.get<string>('SALT_ROUNDS')),
|
||||
);
|
||||
|
||||
return bcrypt.hashSync(password, salt);
|
||||
}
|
||||
|
||||
comparar(password: string, passwordDb: string) {
|
||||
return bcrypt.compareSync(password, passwordDb);
|
||||
}
|
||||
}
|
@ -8,9 +8,9 @@ export class OperadorUpdateDto {
|
||||
@IsOptional()
|
||||
activo?: boolean;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
operador?: string;
|
||||
// @IsString()
|
||||
// @IsOptional()
|
||||
// operador?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
|
@ -3,12 +3,14 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { OperadorController } from './operador.controller';
|
||||
import { OperadorService } from './operador.service';
|
||||
import { Operador } from './entity/operador.entity';
|
||||
import { BcryptModule } from 'src/bcrypt/bcrypt.module';
|
||||
import { InstitucionModule } from 'src/institucion/institucion.module';
|
||||
import { TipoUsuarioModule } from 'src/tipo-usuario/tipo-usuario.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Operador]),
|
||||
BcryptModule,
|
||||
InstitucionModule,
|
||||
TipoUsuarioModule,
|
||||
],
|
||||
|
@ -7,6 +7,8 @@ import {
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Operador } from './entity/operador.entity';
|
||||
import { Institucion } from '../institucion/entity/institucion.entity';
|
||||
import { BcryptService } from 'src/bcrypt/bcrypt.service';
|
||||
import { InstitucionService } from 'src/institucion/institucion.service';
|
||||
import { TipoUsuarioService } from 'src/tipo-usuario/tipo-usuario.service';
|
||||
|
||||
@ -14,22 +16,23 @@ import { TipoUsuarioService } from 'src/tipo-usuario/tipo-usuario.service';
|
||||
export class OperadorService {
|
||||
constructor(
|
||||
@InjectRepository(Operador) private repository: Repository<Operador>,
|
||||
private isnstitucionService: InstitucionService,
|
||||
private bcryptService: BcryptService,
|
||||
private institucionService: InstitucionService,
|
||||
private tipoUsuarioService: TipoUsuarioService,
|
||||
) {}
|
||||
|
||||
async create(id_institucion: number, operador: string, password: string) {
|
||||
const institucion = await this.isnstitucionService.findById(id_institucion);
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
const tipoUsuario = await this.tipoUsuarioService.findById(4);
|
||||
|
||||
return this.repository
|
||||
.findOne({ operador, institucion })
|
||||
.then((existeOperador) => {
|
||||
.then(async (existeOperador) => {
|
||||
if (existeOperador)
|
||||
throw new ConflictException(
|
||||
'Ya existe un operador con ese nombre, intente de nuevo.',
|
||||
);
|
||||
/* Encriptar password */
|
||||
password = this.bcryptService.encriptar(password);
|
||||
return this.repository.save(
|
||||
this.repository.create({
|
||||
institucion,
|
||||
@ -47,13 +50,14 @@ export class OperadorService {
|
||||
id_institucion?: string;
|
||||
operador?: string;
|
||||
}) {
|
||||
const busqueda: any = {};
|
||||
const busqueda: { operador?: string; institucion?: Institucion } = {};
|
||||
const institucion = filtros.id_institucion
|
||||
? await this.isnstitucionService.findById(Number(filtros.id_institucion))
|
||||
? await this.institucionService.findById(Number(filtros.id_institucion))
|
||||
: null;
|
||||
|
||||
if (filtros.operador) busqueda.operador = filtros.operador;
|
||||
if (filtros.id_institucion) busqueda.institucion = institucion;
|
||||
/* falta página */
|
||||
return this.repository.find(busqueda);
|
||||
}
|
||||
|
||||
@ -73,17 +77,20 @@ export class OperadorService {
|
||||
|
||||
login(operador: string, password: string) {
|
||||
return this.findByOperador(operador).then((operador) => {
|
||||
if (operador.password !== password)
|
||||
throw new UnauthorizedException('Contraseña incorrecta.');
|
||||
if (this.bcryptService.comparar(password, operador.password))
|
||||
throw new UnauthorizedException(
|
||||
'Usuario y/o contraseña incorrectos, trata de nuevo.',
|
||||
);
|
||||
/* Crear JWT y regresarlo */
|
||||
return operador;
|
||||
});
|
||||
}
|
||||
|
||||
async update(attrs: Partial<Operador>) {
|
||||
update(attrs: Partial<Operador>) {
|
||||
return this.findById(attrs.id_operador)
|
||||
.then((operador) => {
|
||||
/* encriptar password */
|
||||
if (attrs.password)
|
||||
attrs.password = this.bcryptService.encriptar(attrs.password);
|
||||
Object.assign(operador, attrs);
|
||||
return this.repository.save(operador);
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user