This repository has been archived by the owner on May 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(login): implement login functionality
- add tests - add login controllers - add validations for login [Finishes #161504670]
- Loading branch information
1 parent
a7369e8
commit 8066a69
Showing
8 changed files
with
640 additions
and
70 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import bcrypt from 'bcrypt-nodejs'; | ||
import models from '../models'; | ||
|
||
const { Users } = models; | ||
/** | ||
* @class UserController | ||
* @description Houses operations on the user data. | ||
*/ | ||
class UserController { | ||
/** | ||
* @description - This method logs in user and return a token. | ||
* @param {object} req - The request object bearing the email and password. | ||
* @param {object} res - The response object that is returned as json. | ||
* @returns {object} - The object with message. | ||
* @memberOf UserController | ||
* @static | ||
*/ | ||
static userLogin(req, res) { | ||
const { email, password } = req.body; | ||
Users.find({ | ||
where: email | ||
}) | ||
.then((userFound) => { | ||
if (!userFound) { | ||
return res.status(401).json({ | ||
errors: { | ||
message: ['Invalid email or password'] | ||
} | ||
}); | ||
} | ||
if (bcrypt.compareSync(password, userFound.password)) { | ||
const { id } = userFound; | ||
return res.status(200).json({ | ||
status: 'success', | ||
message: 'you are logged in', | ||
user: { | ||
userFound, | ||
token: createToken(id, email) | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default UserController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @class UserValidation | ||
* @description Helps perform validations on user request body. | ||
*/ | ||
class UserValidation { | ||
/** | ||
* @description - This method validates for email and password field. | ||
* @param {object} req - The request object to be validated. | ||
* @param {object} res - Th response object to be sent to user. | ||
* @param {object} next - The callback function to the next middleware. | ||
* @returns {object} - The error object with message. | ||
* @memberOf UserValidation | ||
* @static | ||
*/ | ||
static validateUserLogin(req, res) { | ||
const { email, password } = req.body; | ||
if (!email) { | ||
res.status(422).json({ | ||
errors: { | ||
message: ['please enter email'] | ||
} | ||
}); | ||
} | ||
if (!password) { | ||
res.status(422).json({ | ||
errors: { | ||
message: ['please enter password'] | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default UserValidation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import app from '../../../index'; | ||
|
||
chai.use(chaiHttp); | ||
chai.should(); | ||
|
||
const loginUrl = '/api/users/login'; | ||
|
||
describe('Testing Login feature -Integration testng', () => { | ||
it( | ||
'should display error message if user logins with empty field', | ||
(done) => { | ||
chai.request(app) | ||
.post(loginUrl) | ||
.send({ | ||
email: '', | ||
password: '' | ||
}) | ||
.end((err, res) => { | ||
res.status.should.be.eql(422); | ||
res.body.should.be.eql({ | ||
errors: { | ||
message: ['please enter email'] | ||
} | ||
}); | ||
}); | ||
done(); | ||
} | ||
); | ||
it( | ||
'should display error message if user logins with empty password', | ||
(done) => { | ||
chai.request(app) | ||
.post(loginUrl) | ||
.send({ | ||
email: 'augustineezinwa@gmail.com', | ||
password: '' | ||
}) | ||
.end((err, res) => { | ||
res.status.should.be.eql(422); | ||
res.body.should.be.eql({ | ||
errors: { | ||
message: ['please enter password'] | ||
} | ||
}); | ||
}); | ||
done(); | ||
} | ||
); | ||
it( | ||
'should display error message if user logins with invalid credentials', | ||
(done) => { | ||
chai.request(app) | ||
.post(loginUrl) | ||
.send({ | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'fishdonek4' | ||
}) | ||
.end((err, res) => { | ||
res.status.should.be.eql(422); | ||
res.body.should.be.eql({ | ||
errors: { | ||
message: ['Invalid email or password'] | ||
} | ||
}); | ||
}); | ||
done(); | ||
} | ||
); | ||
it( | ||
'should login a user', | ||
(done) => { | ||
chai.request(app) | ||
.post(loginUrl) | ||
.send({ | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'fishdonek4' | ||
}) | ||
.end((err, res) => { | ||
res.status.should.be.eql(200); | ||
res.body.should.be.eql({ | ||
errors: { | ||
message: ['Invalid email or password'] | ||
} | ||
}); | ||
}); | ||
done(); | ||
} | ||
); | ||
}); |