Skip to content

Commit

Permalink
Merge pull request #7 from isdi-coders-2023/feature/usersFiles
Browse files Browse the repository at this point in the history
Fix the problems with register and login methods
  • Loading branch information
FranciscoHernandez92 authored May 6, 2024
2 parents 7ed651d + 5edc453 commit 3898e30
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 48 deletions.
10 changes: 1 addition & 9 deletions src/controllers/users.controller/users.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ describe('UserController', () => {
} as unknown as Response;
const mockNext = jest.fn();

const mockUser = {
email: '[email protected]',
password: await Auth.hash('password123'),
};
Auth.compare = jest.fn().mockResolvedValueOnce(true);
Auth.signJwt = jest.fn().mockReturnValueOnce('mockToken');

Expand Down Expand Up @@ -70,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')
);
});
});
Expand Down
12 changes: 5 additions & 7 deletions src/controllers/users.controller/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down
50 changes: 25 additions & 25 deletions src/entities/projects.entitty/projects.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
5 changes: 4 additions & 1 deletion src/routers/users.routers/user.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion src/services/auth.services/auth.services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
9 changes: 4 additions & 5 deletions src/services/auth.services/auth.services.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 3898e30

Please sign in to comment.