Skip to content

Commit

Permalink
fix(explicited-errors): translated empty login's error (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
CocoChart committed Oct 6, 2021
1 parent b5dda03 commit 19464cd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/controllers/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { validateBody } from '../../middlewares/validation';
import { filterUser } from '../../utils/filters';
import { forbidden, success, unauthenticated } from '../../utils/responses';
import { generateToken } from '../../utils/users';
import { Error } from '../../types';
import { Error as ResponseError } from '../../types';
import { fetchUser } from '../../operations/user';
import * as validators from '../../utils/validators';

Expand All @@ -16,7 +16,7 @@ export default [
...isNotAuthenticated,
validateBody(
Joi.object({
login: Joi.string().required(),
login: Joi.string().required().error(new Error(ResponseError.EmptyLogin)),
password: validators.password.required(),
}),
),
Expand All @@ -33,30 +33,30 @@ export default [
} else if (!validators.username.validate(login).error) {
field = 'username';
} else {
return unauthenticated(response, Error.InvalidCredentials);
return unauthenticated(response, ResponseError.InvalidCredentials);
}

const user = await fetchUser(login, field);

// Checks if the user exists
if (!user) {
return unauthenticated(response, Error.InvalidCredentials);
return unauthenticated(response, ResponseError.InvalidCredentials);
}

if (user.registerToken) {
return forbidden(response, Error.EmailNotConfirmed);
return forbidden(response, ResponseError.EmailNotConfirmed);
}

if (user.type === UserType.attendant) {
return forbidden(response, Error.LoginAsAttendant);
return forbidden(response, ResponseError.LoginAsAttendant);
}

// Compares the hash from the password given
const isPasswordValid = await bcrpyt.compare(password, user.password);

// If the password is not valid, rejects the request
if (!isPasswordValid) {
return unauthenticated(response, Error.InvalidCredentials);
return unauthenticated(response, ResponseError.InvalidCredentials);
}

const token = generateToken(user);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export const enum Error {
NoQRCode = "Le QR code n'existe pas",

InvalidCart = 'Le contenu de la commande est invalide',
EmptyLogin = "Le nom d'utilisateur ne peut pas être vide",

// 401
// The user credentials were refused or not provided
Expand Down
9 changes: 9 additions & 0 deletions tests/auth/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ describe('POST /auth/login', () => {
.expect(401, { error: Error.InvalidCredentials });
});

it('should return an error as the login is empty', async () => {
await request(app)
.post('/auth/login')
.send({
password: user.password,
})
.expect(400, { error: Error.EmptyLogin });
});

// This case should never happen
it('should error because the user is an attendant', async () => {
const visitorEmail = '[email protected]';
Expand Down

0 comments on commit 19464cd

Please sign in to comment.