Se corigieron errores
This commit is contained in:
+3
-6
@@ -8,10 +8,6 @@ import { EquipoModule } from './equipo/equipo.module';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { AppController } from './app.controller';
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
@@ -26,15 +22,16 @@ import { AppController } from './app.controller';
|
||||
database: process.env.DB_NAME,
|
||||
autoLoadEntities: true,
|
||||
synchronize: true,
|
||||
logger: 'advanced-console',
|
||||
logging: 'all',
|
||||
}),
|
||||
UsuariosModule,
|
||||
|
||||
MovimientoModule,
|
||||
EquipoModule,
|
||||
AuthModule
|
||||
AuthModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
+12
-11
@@ -3,22 +3,23 @@ import { AuthController } from './auth.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { UsuariosModule } from 'src/usuarios/usuarios.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
|
||||
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { JwtStrategy } from './jwt.strategy';
|
||||
|
||||
@Module({
|
||||
imports:[
|
||||
imports: [
|
||||
UsuariosModule,
|
||||
JwtModule,
|
||||
JwtModule.register({
|
||||
global:true,
|
||||
secret: process.env.JWT,
|
||||
signOptions:{expiresIn:"1d"},
|
||||
|
||||
JwtModule.registerAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: (config: ConfigService) => ({
|
||||
secret: config.get<string>('JWT'), // lee la variable del .env
|
||||
signOptions: { expiresIn: '1d' },
|
||||
}),
|
||||
}),
|
||||
],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService],
|
||||
exports:[AuthService]
|
||||
providers: [AuthService, JwtStrategy],
|
||||
exports: [AuthService],
|
||||
})
|
||||
export class AuthModule {}
|
||||
|
||||
+49
-51
@@ -1,4 +1,9 @@
|
||||
import { BadRequestException, Injectable, UnauthorizedException, InternalServerErrorException } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
InternalServerErrorException,
|
||||
} from '@nestjs/common';
|
||||
import { UsuariosService } from 'src/usuarios/usuarios.service';
|
||||
import * as argon2 from 'argon2';
|
||||
import { LoginDto } from './dto/login.dto';
|
||||
@@ -7,64 +12,57 @@ import { JwtService } from '@nestjs/jwt';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private readonly usuarioService:UsuariosService,
|
||||
private readonly jwtService:JwtService
|
||||
){}
|
||||
constructor(
|
||||
private readonly usuarioService: UsuariosService,
|
||||
private readonly jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
async registro({nombre, contraseña,tipoUsuario}: CreateUsuarioDto){
|
||||
|
||||
const usuario = await this.usuarioService.findOneByName(nombre);
|
||||
if(usuario){
|
||||
throw new BadRequestException("Nombre de usuario ya existente");
|
||||
}
|
||||
|
||||
try{
|
||||
const hashedContraseña = await argon2.hash(contraseña);
|
||||
|
||||
await this.usuarioService.create({
|
||||
nombre,
|
||||
contraseña: hashedContraseña,
|
||||
tipoUsuario,
|
||||
});
|
||||
|
||||
return {
|
||||
message: "Usuario registrado exitosamente"
|
||||
};
|
||||
}catch(error){
|
||||
|
||||
throw new InternalServerErrorException('Error al crear el usuario');
|
||||
}
|
||||
async registro({ nombre, contraseña, tipoUsuario }: CreateUsuarioDto) {
|
||||
const usuario = await this.usuarioService.findOneByName(nombre);
|
||||
if (usuario) {
|
||||
throw new BadRequestException('Nombre de usuario ya existente');
|
||||
}
|
||||
|
||||
async login({nombre, contraseña}:LoginDto){
|
||||
const usuario= await this.usuarioService.findOneByName(nombre)
|
||||
if(!usuario){
|
||||
throw new UnauthorizedException("Usuario no encontrado");
|
||||
try {
|
||||
const hashedContraseña = await argon2.hash(contraseña);
|
||||
|
||||
}
|
||||
const contraseñaValida= await argon2.verify(contraseña, usuario.contraseña);
|
||||
if(!contraseñaValida){
|
||||
throw new UnauthorizedException("Contraseña invalida");
|
||||
await this.usuarioService.create({
|
||||
nombre,
|
||||
contraseña: hashedContraseña,
|
||||
tipoUsuario,
|
||||
});
|
||||
|
||||
}
|
||||
const dataUser={
|
||||
id:usuario.id_usuario,
|
||||
nombre:usuario.nombre,
|
||||
tipoUsuario:usuario.tipoUsuario.id_tipo_usuario
|
||||
}
|
||||
|
||||
const token=await this.jwtService.sign(dataUser)
|
||||
return{
|
||||
token:token,
|
||||
};
|
||||
return {
|
||||
message: 'Usuario registrado exitosamente',
|
||||
};
|
||||
} catch (error) {
|
||||
throw new InternalServerErrorException('Error al crear el usuario');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
async login({ nombre, contraseña }: LoginDto) {
|
||||
const usuario = await this.usuarioService.findOneByName(nombre);
|
||||
if (!usuario) {
|
||||
throw new UnauthorizedException('Usuario no encontrado');
|
||||
}
|
||||
|
||||
const contraseñaValida = await argon2.verify(
|
||||
usuario.contraseña,
|
||||
contraseña,
|
||||
);
|
||||
|
||||
if (!contraseñaValida) {
|
||||
throw new UnauthorizedException('Contraseña invalida');
|
||||
}
|
||||
const dataUser = {
|
||||
id: usuario.id_usuario,
|
||||
nombre: usuario.nombre,
|
||||
tipoUsuario: usuario.tipoUsuario.id_tipo_usuario,
|
||||
};
|
||||
|
||||
const token = await this.jwtService.sign(dataUser);
|
||||
return {
|
||||
token: token,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
|
||||
import { Controller, Body, Patch, Param, ParseIntPipe, UseGuards,Get, Query } from '@nestjs/common';
|
||||
import {
|
||||
Controller,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
UseGuards,
|
||||
Get,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { EquipoService } from './equipo.service';
|
||||
import { UpdateEquipoDto } from './dto/update-equipo.dto';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
@@ -7,7 +15,7 @@ import { AuthGuard } from '@nestjs/passport';
|
||||
@Controller('equipos')
|
||||
export class EquipoController {
|
||||
constructor(private readonly equipoService: EquipoService) {}
|
||||
@UseGuards(AuthGuard("jwt"))
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Get('buscar')
|
||||
async buscarEquipos(
|
||||
@Query() filtros: any,
|
||||
@@ -17,46 +25,49 @@ export class EquipoController {
|
||||
return this.equipoService.buscarEquipos(filtros, +page, +limit);
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard("jwt"))
|
||||
@Patch("update/:id")
|
||||
update(@Param('id', ParseIntPipe) id:number, @Body() equipoNuevo: UpdateEquipoDto) {
|
||||
return this.equipoService.updateEquipo( equipoNuevo,id);
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Patch('update/:id')
|
||||
update(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Body() equipoNuevo: UpdateEquipoDto,
|
||||
) {
|
||||
return this.equipoService.updateEquipo(equipoNuevo, id);
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard("jwt"))
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Get('usos')
|
||||
findUsos() {
|
||||
return this.equipoService.findAllUsos();
|
||||
}
|
||||
@UseGuards(AuthGuard("jwt"))
|
||||
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Get('marcas')
|
||||
findMarcas() {
|
||||
return this.equipoService.findAllMarcas();
|
||||
}
|
||||
@UseGuards(AuthGuard("jwt"))
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Get('estados')
|
||||
findEstados() {
|
||||
return this.equipoService.findAllEstados();
|
||||
}
|
||||
@UseGuards(AuthGuard("jwt"))
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Get('adscripciones')
|
||||
findAdscripciones() {
|
||||
return this.equipoService.findAllAdscripciones();
|
||||
}
|
||||
@UseGuards(AuthGuard("jwt"))
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Get('tipos-equipo')
|
||||
findTiposEquipo() {
|
||||
return this.equipoService.findAllTiposEquipo();
|
||||
}
|
||||
@UseGuards(AuthGuard("jwt"))
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Get('sistemas-operativos')
|
||||
findSistemasOperativos() {
|
||||
return this.equipoService.findAllSistemasOperativos();
|
||||
}
|
||||
@UseGuards(AuthGuard("jwt"))
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Get('procesadores')
|
||||
findProcesadores() {
|
||||
return this.equipoService.findAllProcesadores();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
import { Movimiento } from 'src/movimiento/entities/movimiento.entity';
|
||||
import {Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn} from 'typeorm';
|
||||
|
||||
|
||||
import {
|
||||
Column,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Tipo_Usuario {
|
||||
@PrimaryGeneratedColumn()
|
||||
id_tipo_usuario: number;
|
||||
@PrimaryGeneratedColumn()
|
||||
id_tipo_usuario: number;
|
||||
|
||||
@Column({length:50, nullable:true})
|
||||
tipo_usuario:string
|
||||
@Column({ length: 50, nullable: true })
|
||||
tipo_usuario: string;
|
||||
|
||||
@OneToMany(() => Usuario, (usuarios) => usuarios.tipoUsuario)
|
||||
usuarios: Usuario[];
|
||||
|
||||
@OneToMany(() => Usuario, (usuarios) => usuarios.tipoUsuario)
|
||||
usuarios: Usuario[];
|
||||
}
|
||||
|
||||
@Entity()
|
||||
@@ -24,15 +27,12 @@ export class Usuario {
|
||||
@Column({ length: 100, nullable: true })
|
||||
nombre: string;
|
||||
|
||||
@Column({length:100,nullable:true})
|
||||
contraseña:string
|
||||
|
||||
|
||||
@ManyToOne(()=>Tipo_Usuario,tipoUsuario=>tipoUsuario.usuarios)
|
||||
tipoUsuario:Tipo_Usuario;
|
||||
|
||||
@OneToMany(()=>Movimiento, movimiento=>movimiento.id_movimiento)
|
||||
movimiento:Movimiento[];
|
||||
@Column({ nullable: true })
|
||||
contraseña: string;
|
||||
|
||||
@ManyToOne(() => Tipo_Usuario, (tipoUsuario) => tipoUsuario.usuarios)
|
||||
tipoUsuario: Tipo_Usuario;
|
||||
|
||||
@OneToMany(() => Movimiento, (movimiento) => movimiento.id_movimiento)
|
||||
movimiento: Movimiento[];
|
||||
}
|
||||
|
||||
|
||||
@@ -4,19 +4,15 @@ import { CreateUsuarioDto } from './dto/create-usuario.dto';
|
||||
|
||||
@Controller('usuarios')
|
||||
export class UsuariosController {
|
||||
constructor(private readonly usuariosService: UsuariosService
|
||||
) {}
|
||||
constructor(private readonly usuariosService: UsuariosService) {}
|
||||
|
||||
@Post()
|
||||
@Post('create')
|
||||
create(@Body() createUsuarioDto: CreateUsuarioDto) {
|
||||
return this.usuariosService.create(createUsuarioDto);
|
||||
}
|
||||
|
||||
@Post()
|
||||
createUser(@Body() tipoUsuario :string){
|
||||
|
||||
@Post('createTipoUser')
|
||||
createUser(@Body() tipoUsuario: string) {
|
||||
return this.usuariosService.createUser(tipoUsuario);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -9,36 +9,32 @@ export class UsuariosService {
|
||||
constructor(
|
||||
@InjectRepository(Usuario)
|
||||
private readonly usuarioRepository: Repository<Usuario>,
|
||||
|
||||
|
||||
@InjectRepository(Tipo_Usuario)
|
||||
private readonly tipoUsuarioRepository: Repository<Tipo_Usuario>
|
||||
){}
|
||||
private readonly tipoUsuarioRepository: Repository<Tipo_Usuario>,
|
||||
) {}
|
||||
async create(createUsuarioDto: CreateUsuarioDto) {
|
||||
|
||||
let user= await this.usuarioRepository.create({
|
||||
nombre:createUsuarioDto.nombre,
|
||||
contraseña:createUsuarioDto.contraseña,
|
||||
tipoUsuario:{
|
||||
id_tipo_usuario:createUsuarioDto.tipoUsuario
|
||||
}
|
||||
})
|
||||
let user = await this.usuarioRepository.create({
|
||||
nombre: createUsuarioDto.nombre,
|
||||
contraseña: createUsuarioDto.contraseña,
|
||||
tipoUsuario: {
|
||||
id_tipo_usuario: createUsuarioDto.tipoUsuario,
|
||||
},
|
||||
});
|
||||
|
||||
return await this.usuarioRepository.save(user);
|
||||
}
|
||||
async findOneByName(nombre: string) {
|
||||
return await this.usuarioRepository.findOne({where:{nombre}});
|
||||
async findOneByName(nombre: string) {
|
||||
return await this.usuarioRepository.findOne({
|
||||
where: { nombre },
|
||||
relations: ['tipoUsuario'],
|
||||
});
|
||||
}
|
||||
|
||||
async createUser(tipoUsuario:string){
|
||||
let user=await this.tipoUsuarioRepository.create({
|
||||
tipo_usuario:tipoUsuario
|
||||
})
|
||||
return await this.tipoUsuarioRepository.save(user)
|
||||
async createUser(tipoUsuario: string) {
|
||||
let user = await this.tipoUsuarioRepository.create({
|
||||
tipo_usuario: tipoUsuario,
|
||||
});
|
||||
return await this.tipoUsuarioRepository.save(user);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user