Validation

develop
sundowndev 2018-11-16 18:18:37 +01:00
parent af28952b3a
commit 51f2f49550
4 changed files with 33 additions and 27 deletions

View File

@ -1,27 +0,0 @@
const Joi = require('joi');
const jwt = require('jsonwebtoken');
const secret = require.main.require('./config/secret');
module.exports = (req, res, next) => {
const schema = Joi.object().keys({
access_token: Joi.string().required(),
});
Joi.validate({
access_token: req.headers.authorization,
},
schema, (validateErr) => {
if (validateErr) {
return next({ status: 401, error: validateErr.details });
}
return jwt.verify(req.headers.authorization, secret, (err) => {
if (err) {
return next({ status: 401, message: 'Token error.', error: [err] });
}
return next();
});
});
};

View File

@ -0,0 +1,33 @@
const Joi = require('joi');
module.exports = (req, res, next) => {
const schema = Joi.object().keys({
firstname: Joi.string().min(2),
lastname: Joi.string().min(2),
email: Joi.string().email({ minDomainAtoms: 2 }),
password: Joi.string(),
new_password: Joi.string(),
}).with('password', 'new_password')
.without('firstname', 'password')
.without('lastname', 'password')
.without('email', 'password');
Joi.validate({
firstname: req.body.firstname,
lastname: req.body.lastname,
email: req.body.email,
password: req.body.password,
new_password: req.body.new_password,
},
schema, (validateErr) => {
if (validateErr) {
return next({
status: 400,
message: 'Form is invalid.',
error: validateErr.details,
});
}
return next();
});
};