logins en auth
This commit is contained in:
@@ -1,4 +1,19 @@
|
||||
import { Controller } from '@nestjs/common';
|
||||
import { Body, Controller, Post } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { AuthLoginOperadorDto } from './dto/auth-login-operador.dto';
|
||||
import { AuthLoginUsuarioDto } from './dto/auth-login-usuario.dto';
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthController {}
|
||||
export class AuthController {
|
||||
constructor(private authService: AuthService) {}
|
||||
|
||||
@Post('login_operador')
|
||||
loginOperador(@Body() body: AuthLoginOperadorDto) {
|
||||
return this.authService.loginOperador(body.operador, body.password);
|
||||
}
|
||||
|
||||
@Post('login_usuario')
|
||||
loginUsuario(@Body() body: AuthLoginUsuarioDto) {
|
||||
return this.authService.loginUsuario(body.usuario, body.password);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@ import { JwtModule } from '@nestjs/jwt';
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { BcryptModule } from '../bcrypt/bcrypt.module';
|
||||
import { JwtStrategyService } from './strategy/jwt-strategy.service';
|
||||
import { OperadorModule } from '../operador/operador.module';
|
||||
import { UsuarioModule } from '../usuario/usuario.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -18,6 +21,9 @@ import { JwtStrategyService } from './strategy/jwt-strategy.service';
|
||||
};
|
||||
},
|
||||
}),
|
||||
BcryptModule,
|
||||
OperadorModule,
|
||||
UsuarioModule,
|
||||
],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService, JwtStrategyService],
|
||||
|
||||
@@ -1,9 +1,38 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { BcryptService } from '../bcrypt/bcrypt.service';
|
||||
import { OperadorService } from '../operador/operador.service';
|
||||
import { UsuarioService } from '../usuario/usuario.service';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor() {}
|
||||
constructor(
|
||||
private bcryptService: BcryptService,
|
||||
private operadorService: OperadorService,
|
||||
private usuarioService: UsuarioService,
|
||||
) {}
|
||||
|
||||
validate() {}
|
||||
|
||||
loginUsuario(usuario: string, password: string) {
|
||||
return this.usuarioService.findByUsuario(usuario).then((usuario) => {
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { IsNumberString, IsString } from 'class-validator';
|
||||
|
||||
export class AuthLoginOperadorDto {
|
||||
@IsNumberString()
|
||||
operador: string;
|
||||
|
||||
@IsString()
|
||||
password: string;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { IsNumberString, IsString } from 'class-validator';
|
||||
|
||||
export class AuthLoginUsuarioDto {
|
||||
@IsNumberString()
|
||||
usuario: string;
|
||||
|
||||
@IsString()
|
||||
password: string;
|
||||
}
|
||||
Reference in New Issue
Block a user