Initial LinkedSign platform
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
const Joi = require('joi');
|
||||
|
||||
const isStrongPassword = (value, helpers) => {
|
||||
const rules = [
|
||||
{ test: value.length >= 8, message: 'at least 8 characters' },
|
||||
{ test: /[A-Z]/.test(value), message: 'one uppercase letter' },
|
||||
{ test: /[a-z]/.test(value), message: 'one lowercase letter' },
|
||||
{ test: /\d/.test(value), message: 'one number' },
|
||||
{ test: /[^A-Za-z0-9]/.test(value), message: 'one special character' },
|
||||
];
|
||||
|
||||
const missing = rules.filter((rule) => !rule.test).map((rule) => rule.message);
|
||||
if (missing.length > 0) {
|
||||
return helpers.message(`Password must include ${missing.join(', ')}.`);
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
const registerSchema = Joi.object({
|
||||
email: Joi.string().email().required(),
|
||||
password: Joi.string().max(100).required().custom(isStrongPassword),
|
||||
});
|
||||
|
||||
const loginSchema = Joi.object({
|
||||
email: Joi.string().email().required(),
|
||||
password: Joi.string().required(),
|
||||
});
|
||||
|
||||
const refreshSchema = Joi.object({
|
||||
refreshToken: Joi.string().required(),
|
||||
});
|
||||
|
||||
const changePasswordSchema = Joi.object({
|
||||
currentPassword: Joi.string().required(),
|
||||
newPassword: Joi.string().max(100).required().custom(isStrongPassword),
|
||||
});
|
||||
|
||||
const validate = (schema) => (req, res, next) => {
|
||||
const { error } = schema.validate(req.body, { abortEarly: false });
|
||||
if (error) {
|
||||
const messages = error.details.map((d) => d.message);
|
||||
return res.status(400).json({ success: false, data: null, error: messages.join('; ') });
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
module.exports = { validate, registerSchema, loginSchema, refreshSchema, changePasswordSchema };
|
||||
Reference in New Issue
Block a user