listo login operador, falta prueba, y admin
This commit is contained in:
parent
86deec7380
commit
233089cbf1
@ -1,25 +1,31 @@
|
|||||||
import { Body, Controller, Post } from '@nestjs/common';
|
import { Body, Controller, Post } from '@nestjs/common';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
|
import { AuthLoginAdminDto } from './dto/auth-login-admin.dto';
|
||||||
import { AuthLoginOperadorDto } from './dto/auth-login-operador.dto';
|
import { AuthLoginOperadorDto } from './dto/auth-login-operador.dto';
|
||||||
import { AuthLoginUsuarioDto } from './dto/auth-login-usuario.dto';
|
import { AuthLoginUsuarioDto } from './dto/auth-login-usuario.dto';
|
||||||
import {ApiTags} from '@nestjs/swagger'
|
import { ApiTags } from '@nestjs/swagger';
|
||||||
|
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
@ApiTags('auth')
|
@ApiTags('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private authService: AuthService) {}
|
constructor(private authService: AuthService) {}
|
||||||
|
|
||||||
@Post('login_operador')
|
@Post('login-admin')
|
||||||
|
loginAdmin(@Body() body: AuthLoginAdminDto) {
|
||||||
|
return this.authService.loginAdmin(body.operador, body.password);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post('login-operador')
|
||||||
loginOperador(@Body() body: AuthLoginOperadorDto) {
|
loginOperador(@Body() body: AuthLoginOperadorDto) {
|
||||||
return this.authService.loginOperador(
|
return this.authService.loginOperador(
|
||||||
Number(body.id_institucion),
|
body.id_institucion,
|
||||||
Number(body.id_modulo),
|
body.id_modulo,
|
||||||
body.operador,
|
body.operador,
|
||||||
body.password,
|
body.password,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('login_usuario')
|
@Post('login-usuario')
|
||||||
loginUsuario(@Body() body: AuthLoginUsuarioDto) {
|
loginUsuario(@Body() body: AuthLoginUsuarioDto) {
|
||||||
return this.authService.loginUsuario(body.usuario, body.password);
|
return this.authService.loginUsuario(body.usuario, body.password);
|
||||||
}
|
}
|
||||||
|
@ -22,19 +22,23 @@ export class AuthService {
|
|||||||
|
|
||||||
validate() {}
|
validate() {}
|
||||||
|
|
||||||
loginUsuario(usuario: string, password: string) {
|
loginAdmin(admin: string, password: string) {
|
||||||
return this.usuarioService.findByUsuario(usuario, false).then((usuario) => {
|
return this.operadorService.findByAdmin(admin, false).then((operador) => {
|
||||||
if (!usuario || !this.bcryptService.comparar(password, usuario.password))
|
if (!operador || !this.bcryptService.comparar(password, operador.password))
|
||||||
throw new UnauthorizedException(
|
throw new UnauthorizedException(
|
||||||
'Usuario y/o password incorrectos, trata de nuevo.',
|
'Usuario y/o password incorrectos, trata de nuevo.',
|
||||||
);
|
);
|
||||||
|
if (!operador.activo)
|
||||||
|
throw new UnauthorizedException(
|
||||||
|
'Esta cuenta se encuentra desactivada.',
|
||||||
|
);
|
||||||
|
|
||||||
const payload: JwtPayload = {
|
const payload: JwtPayload = {
|
||||||
id_usuario: usuario.id_usuario,
|
id_operador: operador.id_operador,
|
||||||
id_tipo_usuario: usuario.tipoUsuario.id_tipo_usuario,
|
id_tipo_usuario: operador.tipoUsuario.id_tipo_usuario,
|
||||||
};
|
};
|
||||||
|
|
||||||
return { usuario, token: this.jwtService.sign(payload) };
|
return { operador, token: this.jwtService.sign(payload) };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,9 +51,12 @@ export class AuthService {
|
|||||||
const modulo = await this.moduloService.findById(id_modulo);
|
const modulo = await this.moduloService.findById(id_modulo);
|
||||||
|
|
||||||
return this.operadorService
|
return this.operadorService
|
||||||
.findByOperador(id_institucion, operador)
|
.findByOperador(operador, id_institucion)
|
||||||
.then((operador) => {
|
.then((operador) => {
|
||||||
if (!this.bcryptService.comparar(password, operador.password))
|
if (
|
||||||
|
!operador ||
|
||||||
|
!this.bcryptService.comparar(password, operador.password)
|
||||||
|
)
|
||||||
throw new UnauthorizedException(
|
throw new UnauthorizedException(
|
||||||
'Usuario y/o password incorrectos, trata de nuevo.',
|
'Usuario y/o password incorrectos, trata de nuevo.',
|
||||||
);
|
);
|
||||||
@ -74,4 +81,20 @@ export class AuthService {
|
|||||||
return { operador, token: this.jwtService.sign(payload) };
|
return { operador, token: this.jwtService.sign(payload) };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loginUsuario(usuario: string, password: string) {
|
||||||
|
return this.usuarioService.findByUsuario(usuario, false).then((usuario) => {
|
||||||
|
if (!usuario || !this.bcryptService.comparar(password, usuario.password))
|
||||||
|
throw new UnauthorizedException(
|
||||||
|
'Usuario y/o password incorrectos, trata de nuevo.',
|
||||||
|
);
|
||||||
|
|
||||||
|
const payload: JwtPayload = {
|
||||||
|
id_usuario: usuario.id_usuario,
|
||||||
|
id_tipo_usuario: usuario.tipoUsuario.id_tipo_usuario,
|
||||||
|
};
|
||||||
|
|
||||||
|
return { usuario, token: this.jwtService.sign(payload) };
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
9
src/auth/dto/auth-login-admin.dto.ts
Normal file
9
src/auth/dto/auth-login-admin.dto.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class AuthLoginAdminDto {
|
||||||
|
@IsString()
|
||||||
|
operador: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
password: string;
|
||||||
|
}
|
@ -2,10 +2,10 @@ import { IsInt, IsString } from 'class-validator';
|
|||||||
|
|
||||||
export class AuthLoginOperadorDto {
|
export class AuthLoginOperadorDto {
|
||||||
@IsInt()
|
@IsInt()
|
||||||
id_institucion: string;
|
id_institucion: number;
|
||||||
|
|
||||||
@IsInt()
|
@IsInt()
|
||||||
id_modulo: string;
|
id_modulo: number;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
operador: string;
|
operador: string;
|
||||||
|
@ -4,7 +4,7 @@ import {
|
|||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Between, Repository } from 'typeorm';
|
||||||
import { Operador } from './entity/operador.entity';
|
import { Operador } from './entity/operador.entity';
|
||||||
import { Institucion } from '../institucion/entity/institucion.entity';
|
import { Institucion } from '../institucion/entity/institucion.entity';
|
||||||
import { BcryptService } from '../bcrypt/bcrypt.service';
|
import { BcryptService } from '../bcrypt/bcrypt.service';
|
||||||
@ -61,6 +61,21 @@ export class OperadorService {
|
|||||||
return this.repository.find(busqueda);
|
return this.repository.find(busqueda);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
findByAdmin(admin: string, noExisteValidar = true) {
|
||||||
|
return this.repository
|
||||||
|
.findOne({
|
||||||
|
where: {
|
||||||
|
operador: admin,
|
||||||
|
tipoUsuario: { id_tipo_usuario: Between(2, 3) },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((admin) => {
|
||||||
|
if (noExisteValidar && !admin)
|
||||||
|
throw new NotFoundException('No existe este admin.');
|
||||||
|
return admin;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
findById(id_operador: number) {
|
findById(id_operador: number) {
|
||||||
return this.repository.findOne({ id_operador }).then((operador) => {
|
return this.repository.findOne({ id_operador }).then((operador) => {
|
||||||
if (!operador) throw new NotFoundException('No existe este operador');
|
if (!operador) throw new NotFoundException('No existe este operador');
|
||||||
@ -68,12 +83,17 @@ export class OperadorService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
findByOperador(id_institucion: number, operador: string) {
|
findByOperador(
|
||||||
|
operador: string,
|
||||||
|
id_institucion: number,
|
||||||
|
noExisteValidar = true,
|
||||||
|
) {
|
||||||
return this.institucionService
|
return this.institucionService
|
||||||
.findById(id_institucion)
|
.findById(id_institucion)
|
||||||
.then((institucion) => this.repository.findOne({ institucion, operador }))
|
.then((institucion) => this.repository.findOne({ institucion, operador }))
|
||||||
.then((operador) => {
|
.then((operador) => {
|
||||||
if (!operador) throw new NotFoundException('No existe este operador.');
|
if (noExisteValidar && !operador)
|
||||||
|
throw new NotFoundException('No existe este operador.');
|
||||||
return operador;
|
return operador;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user