-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from andela/feature/162727466/users-should-be-…
…able-to-reset-password #162727466 Users should be able to reset password
- Loading branch information
Showing
10 changed files
with
556 additions
and
167 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,49 @@ | ||
/** | ||
* Validation Helper | ||
*/ | ||
class Validation { | ||
/** | ||
* checkEmptyField | ||
* @param {string} field | ||
* @returns {boolean} true|false | ||
*/ | ||
static checkEmptyField(field) { | ||
if (!field) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* checkEmptyFields | ||
* @param {array} fields | ||
* @returns {boolean} true|false | ||
*/ | ||
static checkEmptyFields(fields) { | ||
let errors; | ||
let status = true; | ||
for (let i = 0; i < fields.length; i++) { | ||
const field = fields[i]; | ||
if (!field.value) { | ||
status = false; | ||
errors = { field: field.name }; | ||
} | ||
} | ||
if (status !== true) return errors; return status; | ||
} | ||
|
||
/** | ||
* compareValues | ||
* @param {string} field1 | ||
* @param {string} field2 | ||
* @returns {boolean} true|false | ||
*/ | ||
static compareValues(field1, field2) { | ||
if (field1 !== field2) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
export default Validation; |
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,6 @@ | ||
const basePath = (req) => { | ||
const path = `${req.protocol}://${req.headers.host}`; | ||
return path; | ||
}; | ||
|
||
export default basePath; |
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,108 @@ | ||
import Validation from '../helpers/Validation'; | ||
import models from '../db/models'; | ||
import Response from '../helpers/response'; | ||
|
||
const { User } = models; | ||
|
||
/** | ||
* User Middleware Class | ||
*/ | ||
class UserMiddleware { | ||
/** | ||
* VerifyEmail | ||
* @param {object} req | ||
* @param {object} res | ||
* @param {function} next | ||
* @memberof UserMiddleware | ||
* @returns {object} email verification | ||
*/ | ||
static async VerifyEmail(req, res, next) { | ||
try { | ||
const { email } = req.body; | ||
const validateEmail = Validation.checkEmptyField(email); | ||
if (validateEmail !== true) { | ||
const response = new Response( | ||
'Bad Request', | ||
400, | ||
'Email field cannot be left empty', | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
const checkEmail = await User.findOne({ | ||
where: { | ||
email, | ||
}, | ||
attributes: ['username', 'email'], | ||
}); | ||
if (!checkEmail) { | ||
const response = new Response( | ||
'Not Found', | ||
404, | ||
'Email does not exist', | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
req.checkEmail = checkEmail; | ||
next(); | ||
} catch (err) { | ||
const response = new Response( | ||
'Internal server error', | ||
500, | ||
`${err}`, | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
} | ||
|
||
/** | ||
* Validate Password | ||
* @param {object} req | ||
* @param {object} res | ||
* @param {function} next | ||
* @memberof UserMiddleware | ||
* @returns {object} validated password | ||
*/ | ||
static async validatePassword(req, res, next) { | ||
try { | ||
const user = req.verifyUser; | ||
const { email } = user; | ||
const { password, confirmPassword } = req.body; | ||
|
||
const fields = [ | ||
{ name: 'Password', value: password }, | ||
{ name: 'Confirm Password', value: confirmPassword } | ||
]; | ||
|
||
const validate = Validation.checkEmptyFields(fields); | ||
|
||
if (validate !== true) { | ||
const response = new Response( | ||
'Bad Request', | ||
400, | ||
`${validate.field} field cannot be empty`, | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
|
||
if (!Validation.compareValues(password, confirmPassword)) { | ||
const response = new Response( | ||
'Bad Request', | ||
400, | ||
'Passwords do not match' | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
req.email = email; | ||
next(); | ||
} catch (err) { | ||
const response = new Response( | ||
'Internal server error', | ||
500, | ||
`${err}`, | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
} | ||
} | ||
|
||
export default UserMiddleware; |
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
Oops, something went wrong.