login operador listo
This commit is contained in:
parent
d38bd391d0
commit
a3ddd56d3f
@ -1,5 +1,5 @@
|
||||
INSERT INTO institucion (institucion, logo) VALUES ("Facultad de Arquitectura", "url");
|
||||
INSERT INTO institucion (institucion, logo) VALUES ("FES Acatlán", "url");
|
||||
INSERT INTO institucion (institucion, logo) VALUES ("Facultad de Arquitectura", "url");
|
||||
|
||||
INSERT INTO carrera (carrera, id_institucion) VALUES ("Matemáticas Aplicadas y Computación", 2);
|
||||
INSERT INTO carrera (carrera, id_institucion) VALUES ("Arquitectura", 1);
|
||||
|
@ -9,7 +9,12 @@ export class AuthController {
|
||||
|
||||
@Post('login_operador')
|
||||
loginOperador(@Body() body: AuthLoginOperadorDto) {
|
||||
return this.authService.loginOperador(body.operador, body.password);
|
||||
return this.authService.loginOperador(
|
||||
Number(body.id_institucion),
|
||||
Number(body.id_modulo),
|
||||
body.operador,
|
||||
body.password,
|
||||
);
|
||||
}
|
||||
|
||||
@Post('login_usuario')
|
||||
|
@ -6,6 +6,7 @@ import { AuthController } from './auth.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { BcryptModule } from '../bcrypt/bcrypt.module';
|
||||
import { JwtStrategyService } from './strategy/jwt-strategy.service';
|
||||
import { ModuloModule } from '../modulo/modulo.module';
|
||||
import { OperadorModule } from '../operador/operador.module';
|
||||
import { UsuarioModule } from '../usuario/usuario.module';
|
||||
|
||||
@ -22,6 +23,7 @@ import { UsuarioModule } from '../usuario/usuario.module';
|
||||
},
|
||||
}),
|
||||
BcryptModule,
|
||||
ModuloModule,
|
||||
OperadorModule,
|
||||
UsuarioModule,
|
||||
],
|
||||
|
@ -1,13 +1,21 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import {
|
||||
ConflictException,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { BcryptService } from '../bcrypt/bcrypt.service';
|
||||
import { ModuloService } from '../modulo/modulo.service';
|
||||
import { OperadorService } from '../operador/operador.service';
|
||||
import { UsuarioService } from '../usuario/usuario.service';
|
||||
import { JwtPayload } from './dto/jwt-payload';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private bcryptService: BcryptService,
|
||||
private jwtService: JwtService,
|
||||
private moduloService: ModuloService,
|
||||
private operadorService: OperadorService,
|
||||
private usuarioService: UsuarioService,
|
||||
) {}
|
||||
@ -25,14 +33,40 @@ export class AuthService {
|
||||
});
|
||||
}
|
||||
|
||||
loginOperador(operador: string, password: string) {
|
||||
return this.operadorService.findByOperador(operador).then((operador) => {
|
||||
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 loginOperador(
|
||||
id_institucion: number,
|
||||
id_modulo: number,
|
||||
operador: string,
|
||||
password: string,
|
||||
) {
|
||||
const modulo = await this.moduloService.findById(id_modulo);
|
||||
|
||||
return this.operadorService
|
||||
.findByOperador(id_institucion, operador)
|
||||
.then((operador) => {
|
||||
if (!this.bcryptService.comparar(password, operador.password))
|
||||
throw new UnauthorizedException(
|
||||
'Usuario y/o contraseña incorrectos, trata de nuevo.',
|
||||
);
|
||||
if (!operador.activo)
|
||||
throw new UnauthorizedException(
|
||||
'Esta cuenta se encuentra desactivada.',
|
||||
);
|
||||
if (
|
||||
modulo.institucion.id_institucion !=
|
||||
operador.institucion.id_institucion
|
||||
)
|
||||
throw new ConflictException(
|
||||
'El módulo seleccionado no pertenece a la misma institución al la que pertenece el operador.',
|
||||
);
|
||||
|
||||
const payload: JwtPayload = {
|
||||
id_operador: operador.id_operador,
|
||||
id_tipo_usuario: operador.tipoUsuario.id_tipo_usuario,
|
||||
id_modulo: modulo.id_modulo,
|
||||
};
|
||||
|
||||
return { operador, token: this.jwtService.sign(payload) };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,13 @@
|
||||
import { IsNumberString, IsString } from 'class-validator';
|
||||
import { IsInt, IsString } from 'class-validator';
|
||||
|
||||
export class AuthLoginOperadorDto {
|
||||
@IsNumberString()
|
||||
@IsInt()
|
||||
id_institucion: string;
|
||||
|
||||
@IsInt()
|
||||
id_modulo: string;
|
||||
|
||||
@IsString()
|
||||
operador: string;
|
||||
|
||||
@IsString()
|
||||
|
9
src/auth/dto/jwt-payload.ts
Normal file
9
src/auth/dto/jwt-payload.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export class JwtPayload {
|
||||
id_tipo_usuario: number;
|
||||
|
||||
id_usuario?: number;
|
||||
|
||||
id_operador?: number;
|
||||
|
||||
id_modulo?: number;
|
||||
}
|
@ -9,5 +9,6 @@ import { InstitucionModule } from '../institucion/institucion.module';
|
||||
imports: [TypeOrmModule.forFeature([Modulo]), InstitucionModule],
|
||||
controllers: [ModuloController],
|
||||
providers: [ModuloService],
|
||||
exports: [ModuloService],
|
||||
})
|
||||
export class ModuloModule {}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { IsString } from 'class-validator';
|
||||
import { IsNumberString } from 'class-validator';
|
||||
|
||||
export class OperadorDto {
|
||||
@IsString()
|
||||
operador: string;
|
||||
@IsNumberString()
|
||||
id_operador: string;
|
||||
}
|
||||
|
@ -25,11 +25,15 @@ export class Operador {
|
||||
@Column({ type: String, nullable: false, length: 60 })
|
||||
password: string;
|
||||
|
||||
@ManyToOne(() => Institucion, (institucion) => institucion.operadores)
|
||||
@ManyToOne(() => Institucion, (institucion) => institucion.operadores, {
|
||||
eager: true,
|
||||
})
|
||||
@JoinColumn({ name: 'id_institucion' })
|
||||
institucion: Institucion;
|
||||
|
||||
@ManyToOne(() => TipoUsuario, (tipoUsuario) => tipoUsuario.operadores)
|
||||
@ManyToOne(() => TipoUsuario, (tipoUsuario) => tipoUsuario.operadores, {
|
||||
eager: true,
|
||||
})
|
||||
@JoinColumn({ name: 'id_tipo_usuario' })
|
||||
tipoUsuario: TipoUsuario;
|
||||
|
||||
|
@ -19,7 +19,7 @@ export class OperadorController {
|
||||
|
||||
@Get('operador')
|
||||
operador(@Query() query: OperadorDto) {
|
||||
return this.operadorService.findByOperador(query.operador);
|
||||
return this.operadorService.findById(Number(query.id_operador));
|
||||
}
|
||||
|
||||
@Get('operadores')
|
||||
|
@ -68,11 +68,14 @@ export class OperadorService {
|
||||
});
|
||||
}
|
||||
|
||||
findByOperador(operador: string) {
|
||||
return this.repository.findOne({ operador }).then((operador) => {
|
||||
if (!operador) throw new NotFoundException('No existe este operador');
|
||||
return operador;
|
||||
});
|
||||
findByOperador(id_institucion: number, operador: string) {
|
||||
return this.institucionService
|
||||
.findById(id_institucion)
|
||||
.then((institucion) => this.repository.findOne({ institucion, operador }))
|
||||
.then((operador) => {
|
||||
if (!operador) throw new NotFoundException('No existe este operador.');
|
||||
return operador;
|
||||
});
|
||||
}
|
||||
|
||||
update(attrs: Partial<Operador>) {
|
||||
|
Loading…
Reference in New Issue
Block a user