Use AJV, refactor a lot
This commit is contained in:
@@ -5,16 +5,30 @@
|
||||
const bcrypt = require(`bcryptjs`);
|
||||
|
||||
/**
|
||||
* Validate email address
|
||||
* Build a custom error
|
||||
* @param {string} message Error message
|
||||
* @param {number} code Error code, should be HTTP status code
|
||||
*/
|
||||
const validateEmailAddress = (email) => {
|
||||
var re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||
return re.test(String(email).toLowerCase());
|
||||
const customError = (message, code) => {
|
||||
const error = new Error(message);
|
||||
error.code = code;
|
||||
return error;
|
||||
};
|
||||
|
||||
/**
|
||||
* Build a custom validation error
|
||||
* @param {array[string]} validationErrors Validation errors
|
||||
*/
|
||||
const validationError = (validationErrors) => {
|
||||
let error = new Error("Validation error(s)");
|
||||
error.validationErrors = validationErrors;
|
||||
error.code = 400;
|
||||
return error;
|
||||
};
|
||||
|
||||
/**
|
||||
* Hash password
|
||||
* @param {*} user
|
||||
* @param {string} password Password to hash
|
||||
*/
|
||||
const hashPassword = (password) => {
|
||||
const salt = bcrypt.genSaltSync(10);
|
||||
@@ -23,13 +37,16 @@ const hashPassword = (password) => {
|
||||
|
||||
/**
|
||||
* Compare password
|
||||
* @param {string} candidatePassword Hashed password supplied by user
|
||||
* @param {string} trustedPassword Hashed password on record for user
|
||||
*/
|
||||
const comparePassword = (candidatePassword, trustedPassword) => {
|
||||
return bcrypt.compareSync(candidatePassword, trustedPassword);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
customError,
|
||||
validationError,
|
||||
hashPassword,
|
||||
comparePassword,
|
||||
validateEmailAddress,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user