usuario login y encriptacion listo
This commit is contained in:
parent
e5610f0dd3
commit
844f290737
@ -8,5 +8,6 @@ import { Carrera } from './entity/carrera.entity';
|
||||
imports: [TypeOrmModule.forFeature([Carrera])],
|
||||
controllers: [CarreraController],
|
||||
providers: [CarreraService],
|
||||
exports: [CarreraService],
|
||||
})
|
||||
export class CarreraModule {}
|
||||
|
@ -3,9 +3,9 @@ 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';
|
||||
import { BcryptModule } from '../bcrypt/bcrypt.module';
|
||||
import { InstitucionModule } from '../institucion/institucion.module';
|
||||
import { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
@ -55,9 +55,10 @@ export class OperadorService {
|
||||
? await this.institucionService.findById(Number(filtros.id_institucion))
|
||||
: null;
|
||||
|
||||
/* buscar like */
|
||||
if (filtros.operador) busqueda.operador = filtros.operador;
|
||||
if (filtros.id_institucion) busqueda.institucion = institucion;
|
||||
/* falta página */
|
||||
if (institucion) busqueda.institucion = institucion;
|
||||
/* falta página */
|
||||
return this.repository.find(busqueda);
|
||||
}
|
||||
|
||||
@ -77,7 +78,7 @@ export class OperadorService {
|
||||
|
||||
login(operador: string, password: string) {
|
||||
return this.findByOperador(operador).then((operador) => {
|
||||
if (this.bcryptService.comparar(password, operador.password))
|
||||
if (!this.bcryptService.comparar(password, operador.password))
|
||||
throw new UnauthorizedException(
|
||||
'Usuario y/o contraseña incorrectos, trata de nuevo.',
|
||||
);
|
||||
|
@ -3,12 +3,16 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UsuarioController } from './usuario.controller';
|
||||
import { UsuarioService } from './usuario.service';
|
||||
import { Usuario } from './entity/usuario.entity';
|
||||
import { BcryptModule } from '../bcrypt/bcrypt.module';
|
||||
// import { CarreraModule } from '../carrera/carrera.module';
|
||||
import { InstitucionModule } from '../institucion/institucion.module';
|
||||
import { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Usuario]),
|
||||
BcryptModule,
|
||||
// CarreraModule,
|
||||
InstitucionModule,
|
||||
TipoUsuarioModule,
|
||||
],
|
||||
|
@ -1,4 +1,5 @@
|
||||
import {
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
UnauthorizedException,
|
||||
@ -6,6 +7,11 @@ import {
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Usuario } from './entity/usuario.entity';
|
||||
import { Carrera } from '../carrera/entity/carrera.entity';
|
||||
import { Institucion } from '../institucion/entity/institucion.entity';
|
||||
import { TipoUsuario } from '../tipo-usuario/entity/tipo-usuario.entity';
|
||||
import { BcryptService } from '../bcrypt/bcrypt.service';
|
||||
// import { CarreraService } from '../carrera/carrera.service';
|
||||
import { InstitucionService } from '../institucion/institucion.service';
|
||||
import { TipoUsuarioService } from '../tipo-usuario/tipo-usuario.service';
|
||||
|
||||
@ -13,6 +19,8 @@ import { TipoUsuarioService } from '../tipo-usuario/tipo-usuario.service';
|
||||
export class UsuarioService {
|
||||
constructor(
|
||||
@InjectRepository(Usuario) private repository: Repository<Usuario>,
|
||||
private bcryptService: BcryptService,
|
||||
// private carreraService: CarreraService,
|
||||
private institucionService: InstitucionService,
|
||||
private tipoUsuarioService: TipoUsuarioService,
|
||||
) {}
|
||||
@ -24,14 +32,30 @@ export class UsuarioService {
|
||||
id_tipo_usuario?: string;
|
||||
usuario?: string;
|
||||
}) {
|
||||
const busqueda: {
|
||||
carrera?: Carrera;
|
||||
institucion?: Institucion;
|
||||
tipoUsuario?: TipoUsuario;
|
||||
usuario?: string;
|
||||
} = {};
|
||||
const institucion = filtros.id_institucion
|
||||
? this.institucionService.findById(Number(filtros.id_institucion))
|
||||
? await this.institucionService.findById(Number(filtros.id_institucion))
|
||||
: null;
|
||||
const tipoUsuario = filtros.id_tipo_usuario
|
||||
? this.tipoUsuarioService.findById(Number(filtros.id_tipo_usuario))
|
||||
? await this.tipoUsuarioService.findById(Number(filtros.id_tipo_usuario))
|
||||
: null;
|
||||
const updtae = {};
|
||||
/* Terminar cuando se tengan las fuinciones faltantes */
|
||||
// const carrera = filtros.id_carrera
|
||||
// ? await this.carreraService.findById(Number(filtros.id_carrera))
|
||||
// : null;
|
||||
|
||||
/* buscar like */
|
||||
if (filtros.usuario) busqueda.usuario = filtros.usuario;
|
||||
if (institucion) busqueda.institucion = institucion;
|
||||
if (tipoUsuario) busqueda.tipoUsuario = tipoUsuario;
|
||||
// if (carrera) busqueda.carrera = carrera;
|
||||
/* falta página */
|
||||
return this.repository.find(busqueda);
|
||||
}
|
||||
|
||||
findById(id_usuario: number) {
|
||||
@ -48,30 +72,67 @@ export class UsuarioService {
|
||||
});
|
||||
}
|
||||
|
||||
generarPassword = () => {
|
||||
const length = 8;
|
||||
const charset =
|
||||
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
const n = charset.length;
|
||||
let password = '';
|
||||
|
||||
for (let i = 0; i < length; i++)
|
||||
password += charset.charAt(Math.floor(Math.random() * n));
|
||||
return password;
|
||||
};
|
||||
|
||||
login(usuario: string, password: string) {
|
||||
return this.findByUsuario(usuario).then((usuario) => {
|
||||
if (password !== usuario.password)
|
||||
throw new UnauthorizedException('Contraseña incorrecta.');
|
||||
if (!this.bcryptService.comparar(password, usuario.password))
|
||||
throw new UnauthorizedException(
|
||||
'Usuario y/o contraseña incorrectos, trata de nuevo.',
|
||||
);
|
||||
/* Crear JWT y regresarlo */
|
||||
return usuario;
|
||||
});
|
||||
}
|
||||
|
||||
registrar(id_usuario: number, telefono: string) {
|
||||
passwordReset(id_usuario: number) {
|
||||
const password = this.generarPassword();
|
||||
|
||||
return this.findById(id_usuario)
|
||||
.then((usuario) => {
|
||||
if (!usuario.password)
|
||||
throw new ConflictException('No existe este usuario.');
|
||||
usuario.password = this.bcryptService.encriptar(password);
|
||||
/* Mandar correo con contraseña */
|
||||
return this.repository.save(usuario);
|
||||
})
|
||||
.then((_) => ({
|
||||
message: 'Se reseteo correctamente la constraseña del usuario.',
|
||||
}));
|
||||
}
|
||||
|
||||
registrar(id_usuario: number, telefono: string) {
|
||||
const password = this.generarPassword();
|
||||
|
||||
return this.findById(id_usuario)
|
||||
.then((usuario) => {
|
||||
if (usuario.password)
|
||||
throw new ConflictException(
|
||||
'Ya fue registrado este número de cuenta.',
|
||||
);
|
||||
usuario.telefono = telefono;
|
||||
/* crear contraseña, encriptarla y mandarla por correo */
|
||||
usuario.password = 'hola';
|
||||
usuario.password = this.bcryptService.encriptar(password);
|
||||
/* Mandar correo con contraseña */
|
||||
return this.repository.save(usuario);
|
||||
})
|
||||
.then((_) => ({ message: 'Se creo correctamente un usuario.' }));
|
||||
}
|
||||
|
||||
async update(attrs: Partial<Usuario>) {
|
||||
update(attrs: Partial<Usuario>) {
|
||||
return this.findById(attrs.id_usuario)
|
||||
.then((usuario) => {
|
||||
/* crear y encriptar password en caso de cambio de password */
|
||||
if (!usuario.password)
|
||||
throw new ConflictException('No existe este usuario.');
|
||||
Object.assign(usuario, attrs);
|
||||
return this.repository.save(usuario);
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user