From 0b4c3d56d3a950590de6c233b65240011fa8547b Mon Sep 17 00:00:00 2001 From: arturo Date: Tue, 8 Sep 2020 11:29:11 -0500 Subject: [PATCH] cambios para completar user --- package-lock.json | 99 ++++++++++++++++++++++++++++++++++++++ package.json | 2 + src/catalogos.ts | 8 +++ src/controller/Auth.ts | 8 +++ src/controller/Prestamo.ts | 21 ++++++-- src/controller/Usuario.ts | 9 +++- src/entity/Mesa.ts | 17 ++++++- src/entity/Usuario.ts | 2 + src/middleware/auth.ts | 17 +++++++ src/routes/usuario.ts | 3 +- 10 files changed, 179 insertions(+), 7 deletions(-) create mode 100644 src/controller/Auth.ts create mode 100644 src/middleware/auth.ts diff --git a/package-lock.json b/package-lock.json index d6496a3..f98fb9d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -164,6 +164,14 @@ "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", "integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4=" }, + "@types/jsonwebtoken": { + "version": "8.5.0", + "resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-8.5.0.tgz", + "integrity": "sha512-9bVao7LvyorRGZCw0VmH/dr7Og+NdjYSsKAxB43OQoComFbBgsEpoR9JW6+qSq/ogwVBg8GI2MfAlk4SYI4OLg==", + "requires": { + "@types/node": "*" + } + }, "@types/mime": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.3.tgz", @@ -617,6 +625,11 @@ "ieee754": "^1.1.4" } }, + "buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=" + }, "buffer-from": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", @@ -1096,6 +1109,14 @@ "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=" }, + "ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "requires": { + "safe-buffer": "^5.0.1" + } + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -2172,6 +2193,49 @@ "minimist": "^1.2.0" } }, + "jsonwebtoken": { + "version": "8.5.1", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz", + "integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==", + "requires": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^5.6.0" + }, + "dependencies": { + "semver": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" + } + } + }, + "jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "requires": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "keyv": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/keyv/-/keyv-3.1.0.tgz", @@ -2224,6 +2288,41 @@ "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==", "dev": true }, + "lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8=" + }, + "lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY=" + }, + "lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M=" + }, + "lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w=" + }, + "lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=" + }, + "lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=" + }, + "lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=" + }, "log-symbols": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.0.0.tgz", diff --git a/package.json b/package.json index a63facc..c8dd2d0 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@types/cors": "^2.8.7", "@types/express": "^4.17.7", "@types/hapi__joi": "^17.1.4", + "@types/jsonwebtoken": "^8.5.0", "@types/multer": "^1.4.4", "@types/papaparse": "^5.0.6", "bcrypt": "^5.0.0", @@ -29,6 +30,7 @@ "cors": "^2.8.5", "csv-parser": "^2.3.3", "express": "^4.17.1", + "jsonwebtoken": "^8.5.1", "log-symbols": "^4.0.0", "moment": "^2.27.0", "multer": "^1.4.2", diff --git a/src/catalogos.ts b/src/catalogos.ts index c3bb120..1240661 100644 --- a/src/catalogos.ts +++ b/src/catalogos.ts @@ -1,9 +1,11 @@ import { createConnection, getRepository } from 'typeorm' import { Horario } from './entity/Horario' +import { Mesa } from './entity/Mesa' createConnection() .then(async (connection) => { const horarioRepository = getRepository(Horario) + const mesaRepository = getRepository(Mesa) const horarios = [ '8:00-10:00', '10:00-12:00', @@ -18,6 +20,12 @@ createConnection() console.log(horario) await horarioRepository.save(horario) } + for (let i = 1; i < 30; i++) { + const mesa = new Mesa() + mesa.nombre = String(i) + await mesaRepository.save(mesa) + } + process.exit() }) .catch((error) => console.log('TypeORM connection error: ', error)) diff --git a/src/controller/Auth.ts b/src/controller/Auth.ts new file mode 100644 index 0000000..8db44f8 --- /dev/null +++ b/src/controller/Auth.ts @@ -0,0 +1,8 @@ +import * as jwt from 'jsonwebtoken' +export class AuthController { + static async signUsuario (type: string, usuarioId: string) { + const secret = 'Holabeb' + const token = jwt.sign({ type, usuarioId }, secret, { expiresIn: '1h' }) + return token + } +} diff --git a/src/controller/Prestamo.ts b/src/controller/Prestamo.ts index f068dd4..e18a10d 100644 --- a/src/controller/Prestamo.ts +++ b/src/controller/Prestamo.ts @@ -1,5 +1,5 @@ import { Request, Response } from 'express' -import { getRepository } from 'typeorm' +import { getRepository, QueryBuilder } from 'typeorm' import { Equipo } from '@src/entity/Equipo' import { Prestamo } from '@src/entity/Prestamo' import { Usuario } from '@src/entity/Usuario' @@ -133,12 +133,25 @@ export class PrestamoController { static async estatusPrestamo (req: Request, res: Response) { const repository = getRepository(Prestamo) - const { prestamoId } = req.query + const { usuarioId } = req.query + console.log(usuarioId) - const prestamo = await repository.findOne(+prestamoId) + const prestamo = await repository + .createQueryBuilder('prestamo') + .leftJoinAndSelect('prestamo.usuario', 'usuario') + .where('usuario.id = :usuarioId', { usuarioId }) + .andWhere('prestamo.activo = :activo', { activo: true }) + .getOne() + // const prestamo = await repository.find({ + // relations: ['usuario'], + // where: { + // usuarioId, + // activo: true + // } + // }) if (!prestamo) { - res.status(400).json({ err: 'El prestamo no existe' }) + res.status(400).json({ err: 'El usuario no tiene prestamos activos' }) return } diff --git a/src/controller/Usuario.ts b/src/controller/Usuario.ts index 7536a5a..70c4bd0 100644 --- a/src/controller/Usuario.ts +++ b/src/controller/Usuario.ts @@ -2,6 +2,7 @@ import { Usuario } from '@src/entity/Usuario' import { Request, Response } from 'express' import { validate } from 'class-validator' import { getRepository } from 'typeorm' +import { AuthController } from './Auth' export class UsuarioController { static async create (req: Request, res: Response) { console.log(req.file) @@ -91,11 +92,17 @@ export class UsuarioController { if (!usuario.checkIfUnencryptedPasswordIsValid(secreto)) { throw new Error('Usuario o *contraseƱa invalidos') } + const token = await AuthController.signUsuario( + usuario.interno ? 'usuarioInterno' : 'usuarioExterno', + usuario.id + ) + console.log(token) res.status(200).json({ nombre: usuario.nombre, apellidoPaterno: usuario.apellidoPaterno, apellidoMaterno: usuario.apellidoMaterno, - interno: usuario.interno + interno: usuario.interno, + token }) } catch (err) { res.status(400).send(err.message) diff --git a/src/entity/Mesa.ts b/src/entity/Mesa.ts index 06555a7..9eef0dc 100644 --- a/src/entity/Mesa.ts +++ b/src/entity/Mesa.ts @@ -1,9 +1,24 @@ -import { Entity, OneToMany } from 'typeorm' +import { + Entity, + OneToMany, + Column, + UpdateDateColumn, + CreateDateColumn +} from 'typeorm' import { Catalogo } from './Catalogo' import { Prestamo } from './Prestamo' @Entity() export class Mesa extends Catalogo { + @Column({ default: false }) + activo: boolean + + @UpdateDateColumn() + updatedAt: Date + + @CreateDateColumn() + createdAt: Date + @OneToMany( (type) => Prestamo, (prestamo) => prestamo.mesa diff --git a/src/entity/Usuario.ts b/src/entity/Usuario.ts index 4330a45..9ba7669 100644 --- a/src/entity/Usuario.ts +++ b/src/entity/Usuario.ts @@ -86,6 +86,8 @@ export class Usuario { @UpdateDateColumn() updatedAt: Date + token: string + @OneToMany( (type) => Reservacion, (reservacion) => reservacion.usuario diff --git a/src/middleware/auth.ts b/src/middleware/auth.ts new file mode 100644 index 0000000..6be3c89 --- /dev/null +++ b/src/middleware/auth.ts @@ -0,0 +1,17 @@ +import * as jwt from 'jsonwebtoken' +import { Request, Response, NextFunction } from 'express' + +const verificaToken = (req: Request, res: Response, next: NextFunction) => { + const token = req.get('authorization') + jwt.verify(token.split(' ')[1], 'Holabeb', (err, decoded) => { + if (err) { + return res.status(401).json({ + status: false, + err: 'jsonwebtoken error' + }) + } + next() + }) +} + +export default verificaToken diff --git a/src/routes/usuario.ts b/src/routes/usuario.ts index d353433..e74e7d4 100644 --- a/src/routes/usuario.ts +++ b/src/routes/usuario.ts @@ -1,6 +1,7 @@ import { Router } from 'express' import { UsuarioController } from '@src/controller/Usuario' import * as multer from 'multer' +import verificaToken from '@src/middleware/auth' var upload = multer({ dest: 'uploads/identifiaciones' }) const router = Router() @@ -9,7 +10,7 @@ router.post('/', upload.single('identificacion'), UsuarioController.create) router.post('/login', UsuarioController.login) -router.get('/', UsuarioController.getAll) +router.get('/', [verificaToken], UsuarioController.getAll) router.get('/:id([0-9]+)', UsuarioController.getOne)