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 { TipoUsuario } from './tipo-usuario/entity/tipo-usuario.entity';
|
||||||
import { Usuario } from './usuario/entity/usuario.entity';
|
import { Usuario } from './usuario/entity/usuario.entity';
|
||||||
import { AuthModule } from './auth/auth.module';
|
import { AuthModule } from './auth/auth.module';
|
||||||
|
import { BcryptModule } from './bcrypt/bcrypt.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -59,7 +60,7 @@ import { AuthModule } from './auth/auth.module';
|
|||||||
database: configService.get<string>('DB'),
|
database: configService.get<string>('DB'),
|
||||||
username: configService.get<string>('DB_USER'),
|
username: configService.get<string>('DB_USER'),
|
||||||
password: configService.get<string>('DB_PASSWORD'),
|
password: configService.get<string>('DB_PASSWORD'),
|
||||||
port: configService.get<number>('DB_PORT'),
|
port: Number(configService.get<string>('DB_PORT')),
|
||||||
synchronize: true,
|
synchronize: true,
|
||||||
entities: [
|
entities: [
|
||||||
Carrera,
|
Carrera,
|
||||||
@ -109,6 +110,7 @@ import { AuthModule } from './auth/auth.module';
|
|||||||
TipoUsuarioModule,
|
TipoUsuarioModule,
|
||||||
UsuarioModule,
|
UsuarioModule,
|
||||||
InstitucionTipoCarritoModule,
|
InstitucionTipoCarritoModule,
|
||||||
|
BcryptModule,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
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()
|
@IsOptional()
|
||||||
activo?: boolean;
|
activo?: boolean;
|
||||||
|
|
||||||
@IsString()
|
// @IsString()
|
||||||
@IsOptional()
|
// @IsOptional()
|
||||||
operador?: string;
|
// operador?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
@ -3,12 +3,14 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
|||||||
import { OperadorController } from './operador.controller';
|
import { OperadorController } from './operador.controller';
|
||||||
import { OperadorService } from './operador.service';
|
import { OperadorService } from './operador.service';
|
||||||
import { Operador } from './entity/operador.entity';
|
import { Operador } from './entity/operador.entity';
|
||||||
|
import { BcryptModule } from 'src/bcrypt/bcrypt.module';
|
||||||
import { InstitucionModule } from 'src/institucion/institucion.module';
|
import { InstitucionModule } from 'src/institucion/institucion.module';
|
||||||
import { TipoUsuarioModule } from 'src/tipo-usuario/tipo-usuario.module';
|
import { TipoUsuarioModule } from 'src/tipo-usuario/tipo-usuario.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forFeature([Operador]),
|
TypeOrmModule.forFeature([Operador]),
|
||||||
|
BcryptModule,
|
||||||
InstitucionModule,
|
InstitucionModule,
|
||||||
TipoUsuarioModule,
|
TipoUsuarioModule,
|
||||||
],
|
],
|
||||||
|
@ -7,6 +7,8 @@ import {
|
|||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { Operador } from './entity/operador.entity';
|
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 { InstitucionService } from 'src/institucion/institucion.service';
|
||||||
import { TipoUsuarioService } from 'src/tipo-usuario/tipo-usuario.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 {
|
export class OperadorService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Operador) private repository: Repository<Operador>,
|
@InjectRepository(Operador) private repository: Repository<Operador>,
|
||||||
private isnstitucionService: InstitucionService,
|
private bcryptService: BcryptService,
|
||||||
|
private institucionService: InstitucionService,
|
||||||
private tipoUsuarioService: TipoUsuarioService,
|
private tipoUsuarioService: TipoUsuarioService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async create(id_institucion: number, operador: string, password: string) {
|
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);
|
const tipoUsuario = await this.tipoUsuarioService.findById(4);
|
||||||
|
|
||||||
return this.repository
|
return this.repository
|
||||||
.findOne({ operador, institucion })
|
.findOne({ operador, institucion })
|
||||||
.then((existeOperador) => {
|
.then(async (existeOperador) => {
|
||||||
if (existeOperador)
|
if (existeOperador)
|
||||||
throw new ConflictException(
|
throw new ConflictException(
|
||||||
'Ya existe un operador con ese nombre, intente de nuevo.',
|
'Ya existe un operador con ese nombre, intente de nuevo.',
|
||||||
);
|
);
|
||||||
/* Encriptar password */
|
password = this.bcryptService.encriptar(password);
|
||||||
return this.repository.save(
|
return this.repository.save(
|
||||||
this.repository.create({
|
this.repository.create({
|
||||||
institucion,
|
institucion,
|
||||||
@ -47,13 +50,14 @@ export class OperadorService {
|
|||||||
id_institucion?: string;
|
id_institucion?: string;
|
||||||
operador?: string;
|
operador?: string;
|
||||||
}) {
|
}) {
|
||||||
const busqueda: any = {};
|
const busqueda: { operador?: string; institucion?: Institucion } = {};
|
||||||
const institucion = filtros.id_institucion
|
const institucion = filtros.id_institucion
|
||||||
? await this.isnstitucionService.findById(Number(filtros.id_institucion))
|
? await this.institucionService.findById(Number(filtros.id_institucion))
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
if (filtros.operador) busqueda.operador = filtros.operador;
|
if (filtros.operador) busqueda.operador = filtros.operador;
|
||||||
if (filtros.id_institucion) busqueda.institucion = institucion;
|
if (filtros.id_institucion) busqueda.institucion = institucion;
|
||||||
|
/* falta página */
|
||||||
return this.repository.find(busqueda);
|
return this.repository.find(busqueda);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,17 +77,20 @@ export class OperadorService {
|
|||||||
|
|
||||||
login(operador: string, password: string) {
|
login(operador: string, password: string) {
|
||||||
return this.findByOperador(operador).then((operador) => {
|
return this.findByOperador(operador).then((operador) => {
|
||||||
if (operador.password !== password)
|
if (this.bcryptService.comparar(password, operador.password))
|
||||||
throw new UnauthorizedException('Contraseña incorrecta.');
|
throw new UnauthorizedException(
|
||||||
|
'Usuario y/o contraseña incorrectos, trata de nuevo.',
|
||||||
|
);
|
||||||
/* Crear JWT y regresarlo */
|
/* Crear JWT y regresarlo */
|
||||||
return operador;
|
return operador;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(attrs: Partial<Operador>) {
|
update(attrs: Partial<Operador>) {
|
||||||
return this.findById(attrs.id_operador)
|
return this.findById(attrs.id_operador)
|
||||||
.then((operador) => {
|
.then((operador) => {
|
||||||
/* encriptar password */
|
if (attrs.password)
|
||||||
|
attrs.password = this.bcryptService.encriptar(attrs.password);
|
||||||
Object.assign(operador, attrs);
|
Object.assign(operador, attrs);
|
||||||
return this.repository.save(operador);
|
return this.repository.save(operador);
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user