Skip to content

Commit

Permalink
Merge 6cde92f into fcb8c10
Browse files Browse the repository at this point in the history
  • Loading branch information
rukundoeric committed Jul 9, 2019
2 parents fcb8c10 + 6cde92f commit c890402
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/api/controllers/articlesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class articlesController {
articleId: article.id,
authorId: article.authorId,
moderatorId: user.id,
blockedDay: days[new Date().getDay() - 1],
blockedDay: days[new Date().getDay() - 1] || 'Sunday',
description
};
BlockedArticles.create(object).then(async (responce) => {
Expand Down
65 changes: 31 additions & 34 deletions src/api/controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,44 +144,41 @@ class AuthController {
* @returns {Object} The response object
*/
static async login(req, res) {
User.findAll({
where: {
email: req.body.email
}
}).then((users) => {
if (users[0]) {
if (
!HashHelper.comparePassword(req.body.password, users[0].dataValues.password)
) {
res.status(400).send({
status: 400,
error: {
message: 'Incorrect password'
}
});
} else {
generateToken({
...users[0].dataValues,
password: null,
}).then((token) => {
res.status(200).send({
status: 200,
data: {
message: 'User logged in successful',
token
}
});
});
}
} else {
res.status(404).send({
status: 404,
const { email } = req.body;
const users = await User.findOne({ where: { email } })
|| await User.findOne({ where: { username: email } });
if (users) {
if (
!HashHelper.comparePassword(req.body.password, users.dataValues.password)
) {
res.status(400).send({
status: 400,
error: {
message: 'User with that email does not exist.'
message: 'Incorrect password'
}
});
} else {
generateToken({
...users.dataValues,
password: null,
}).then((token) => {
res.status(200).send({
status: 200,
data: {
message: 'User logged in successful',
token
}
});
});
}
});
} else {
res.status(404).send({
status: 404,
error: {
message: 'User with that email does not exist.'
}
});
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/validationSchemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export default {
newpassword: password
}),
login: Joi.object().keys({
email,
email: Joi.string()
.trim()
.lowercase()
.required()
.min(3)
.label('Username or email are required, they must have at least 3 letters'),
password
}),
signup: Joi.object().keys({
Expand Down
2 changes: 1 addition & 1 deletion test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ describe('User login', () => {
.request(server)
.post('/api/auth/login')
.send({
email: 'rukundogmail.com',
email: 'ru',
password: 'Rukundo1!'
})
.end((err, res) => {
Expand Down

0 comments on commit c890402

Please sign in to comment.