From 3e0eac3e8f57b64d573f5525f5b2bb31b3419e5f Mon Sep 17 00:00:00 2001 From: fran hernandez Date: Mon, 6 May 2024 15:39:57 +0200 Subject: [PATCH 1/2] Fix the problems with register and login methods --- .../users.controller/users.controller.test.ts | 4 -- .../users.controller/users.controller.ts | 12 ++--- .../projects.entitty/projects.entity.ts | 50 +++++++++---------- src/routers/users.routers/user.router.ts | 5 +- .../auth.services/auth.services.test.ts | 2 +- src/services/auth.services/auth.services.ts | 9 ++-- 6 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/controllers/users.controller/users.controller.test.ts b/src/controllers/users.controller/users.controller.test.ts index 226d3c4..79f7c89 100644 --- a/src/controllers/users.controller/users.controller.test.ts +++ b/src/controllers/users.controller/users.controller.test.ts @@ -27,10 +27,6 @@ describe('UserController', () => { } as unknown as Response; const mockNext = jest.fn(); - const mockUser = { - email: 'test@example.com', - password: await Auth.hash('password123'), - }; Auth.compare = jest.fn().mockResolvedValueOnce(true); Auth.signJwt = jest.fn().mockReturnValueOnce('mockToken'); diff --git a/src/controllers/users.controller/users.controller.ts b/src/controllers/users.controller/users.controller.ts index c0b8f48..7f49ef3 100644 --- a/src/controllers/users.controller/users.controller.ts +++ b/src/controllers/users.controller/users.controller.ts @@ -25,11 +25,7 @@ export class UserController extends BaseController< if (!email || !password) { next( - new HttpError( - 400, - 'Bad Request', - 'Email/name and password are required' - ) + new HttpError(400, 'Bad Request', 'Email and password are required') ); return; } @@ -65,13 +61,15 @@ export class UserController extends BaseController< } } - async create(req: Request, res: Response, next: NextFunction) { + async createUser(req: Request, res: Response, next: NextFunction) { const data = req.body as UserCreateAndUpdateDto; - data.password = await Auth.hash(data.password); + data.password = await Auth.hash(data.password, 10); + try { const result = await this.repo.create(data); res.status(201); res.json(result); + await super.create(req, res, next); } catch (error) { next(error); } diff --git a/src/entities/projects.entitty/projects.entity.ts b/src/entities/projects.entitty/projects.entity.ts index 9feac3c..dcd09cc 100644 --- a/src/entities/projects.entitty/projects.entity.ts +++ b/src/entities/projects.entitty/projects.entity.ts @@ -10,29 +10,29 @@ export type Project = { }; type Category = { - geografia: string; - anatomia: string; - matematicas: string; - arte: string; - literatura: string; - fisica: string; - biologia: string; - historia: string; - quimica: string; - musica: string; - economia: string; - filosofia: string; - derecho: string; - idiomas: string; - informatica: string; - geologia: string; - psicologia: string; - contabilidad: string; - astronomia: string; - hosteleria: string; - sociologia: string; - sexologia: string; - ingenieria: string; - arquitectura: string; - paleontologia: string; + geography: string; + anatomy: string; + mathematics: string; + art: string; + literature: string; + physics: string; + biology: string; + history: string; + chemistry: string; + music: string; + economics: string; + philosophy: string; + law: string; + languages: string; + computerScience: string; + geology: string; + psychology: string; + accounting: string; + astronomy: string; + hospitality: string; + sociology: string; + sexology: string; + engineering: string; + architecture: string; + paleontology: string; }; diff --git a/src/routers/users.routers/user.router.ts b/src/routers/users.routers/user.router.ts index 51d89a9..7e1b76b 100644 --- a/src/routers/users.routers/user.router.ts +++ b/src/routers/users.routers/user.router.ts @@ -14,7 +14,10 @@ export class UserRouter { ) { debug('instantiated user router'); - this.router.post('/register', userController.create.bind(userController)); + this.router.post( + '/register', + userController.createUser.bind(userController) + ); this.router.post('/login', userController.login.bind(userController)); diff --git a/src/services/auth.services/auth.services.test.ts b/src/services/auth.services/auth.services.test.ts index 25ef9a0..532d192 100644 --- a/src/services/auth.services/auth.services.test.ts +++ b/src/services/auth.services/auth.services.test.ts @@ -7,7 +7,7 @@ jest.mock('jsonwebtoken'); describe('Given the "static" class Auth', () => { describe('When we use the static method hash', () => { test('Then it should call hash from bcrypt', async () => { - await Auth.hash('test'); + await Auth.hash('test', 10); expect(hash).toHaveBeenCalled(); }); }); diff --git a/src/services/auth.services/auth.services.ts b/src/services/auth.services/auth.services.ts index b35193f..9f217d8 100644 --- a/src/services/auth.services/auth.services.ts +++ b/src/services/auth.services/auth.services.ts @@ -1,7 +1,6 @@ /* eslint-disable @typescript-eslint/no-extraneous-class */ -import { compare, hash } from 'bcrypt'; +import bcrypt from 'bcrypt'; import jwt from 'jsonwebtoken'; -import { HttpError } from '../../middleware/errors.middleware/errors.middleware.js'; export type Payload = { id: string; @@ -12,12 +11,12 @@ export type Payload = { export class Auth { static secret = process.env.SECRET_JWT; - static async hash(value: string) { - return hash(value, 10); + static async hash(password: string, saltRounds: number) { + return bcrypt.hash(password, saltRounds); } static async compare(value: string, hash: string) { - return compare(value, hash); + return bcrypt.compare(value, hash); } static signJwt(payload: Payload) { From 5edc4536ee8615192c7cff5dbb28285efa3d874f Mon Sep 17 00:00:00 2001 From: fran hernandez Date: Mon, 6 May 2024 15:48:27 +0200 Subject: [PATCH 2/2] Fix sonar and audit --- src/controllers/users.controller/users.controller.test.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/controllers/users.controller/users.controller.test.ts b/src/controllers/users.controller/users.controller.test.ts index 79f7c89..33928d8 100644 --- a/src/controllers/users.controller/users.controller.test.ts +++ b/src/controllers/users.controller/users.controller.test.ts @@ -66,11 +66,7 @@ describe('UserController', () => { await controller.login(mockRequest, mockResponse, mockNext); expect(mockNext).toHaveBeenCalledWith( - new HttpError( - 400, - 'Bad Request', - 'Email/name and password are required' - ) + new HttpError(400, 'Bad Request', 'Email and password are required') ); }); });