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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { IsNumberString, IsString } from 'class-validator';
|
||||
|
||||
export class OperadorLoginDto {
|
||||
export class AuthLoginOperadorDto {
|
||||
@IsNumberString()
|
||||
operador: string;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { IsNumberString, IsString } from 'class-validator';
|
||||
|
||||
export class UsuarioLoginDto {
|
||||
export class AuthLoginUsuarioDto {
|
||||
@IsNumberString()
|
||||
usuario: string;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common';
|
||||
import { OperadorService } from './operador.service';
|
||||
import { OperadorCreateDto } from './dto/operador-create.dto';
|
||||
import { OperadorLoginDto } from './dto/operador-login.dto';
|
||||
import { OperadorOperadoresDto } from './dto/operador-operadores.dto';
|
||||
import { OperadorUpdateDto } from './dto/operador-update.dto';
|
||||
import { OperadorDto } from './dto/operador.dto';
|
||||
@@ -18,11 +17,6 @@ export class OperadorController {
|
||||
);
|
||||
}
|
||||
|
||||
@Post('login')
|
||||
login(@Body() body: OperadorLoginDto) {
|
||||
return this.operadorService.login(body.operador, body.password);
|
||||
}
|
||||
|
||||
@Get('operador')
|
||||
operador(@Query() query: OperadorDto) {
|
||||
return this.operadorService.findByOperador(query.operador);
|
||||
|
||||
@@ -16,5 +16,6 @@ import { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module';
|
||||
],
|
||||
controllers: [OperadorController],
|
||||
providers: [OperadorService],
|
||||
exports: [OperadorService],
|
||||
})
|
||||
export class OperadorModule {}
|
||||
|
||||
@@ -2,7 +2,6 @@ import {
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
@@ -76,17 +75,6 @@ export class OperadorService {
|
||||
});
|
||||
}
|
||||
|
||||
login(operador: string, password: string) {
|
||||
return this.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;
|
||||
});
|
||||
}
|
||||
|
||||
update(attrs: Partial<Operador>) {
|
||||
return this.findById(attrs.id_operador)
|
||||
.then((operador) => {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common';
|
||||
import { UsuarioService } from './usuario.service';
|
||||
import { UsuarioEscolaresDto } from './dto/usuario-escolares.dto';
|
||||
import { UsuarioLoginDto } from './dto/usuario-login.dto';
|
||||
import { UsuarioRegistrarDto } from './dto/usuario-registrar.dto';
|
||||
import { UsuarioUpdateDto } from './dto/usuario-update.dto';
|
||||
import { UsuarioUsuariosDto } from './dto/usuario-usuarios.dto';
|
||||
@@ -15,11 +14,6 @@ export class UsuarioController {
|
||||
@Get('escolares')
|
||||
escolares(@Query() query: UsuarioEscolaresDto) {}
|
||||
|
||||
@Post('login')
|
||||
login(@Body() body: UsuarioLoginDto) {
|
||||
return this.usuarioService.login(body.usuario, body.password);
|
||||
}
|
||||
|
||||
@Post('registrar')
|
||||
registrar(@Body() body: UsuarioRegistrarDto) {
|
||||
return this.usuarioService.registrar(body.id_usuario, body.telefono);
|
||||
|
||||
@@ -18,5 +18,6 @@ import { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module';
|
||||
],
|
||||
controllers: [UsuarioController],
|
||||
providers: [UsuarioService],
|
||||
exports: [UsuarioService],
|
||||
})
|
||||
export class UsuarioModule {}
|
||||
|
||||
@@ -2,7 +2,6 @@ import {
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
@@ -84,17 +83,6 @@ export class UsuarioService {
|
||||
return password;
|
||||
};
|
||||
|
||||
login(usuario: string, password: string) {
|
||||
return this.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;
|
||||
});
|
||||
}
|
||||
|
||||
passwordReset(id_usuario: number) {
|
||||
const password = this.generarPassword();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user