Validation middlewares

pull/1/head
sundowndev 2018-11-15 19:27:45 +01:00
parent 664323e911
commit 2c39b55fad
5 changed files with 111 additions and 0 deletions

27
app/validation/auth.js Normal file
View File

@ -0,0 +1,27 @@
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, decoded) => {
if (err) {
return next({ status: 401, message: 'Token error.', error: [err] });
}
return next();
});
});
};

20
app/validation/login.js Normal file
View File

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

View File

@ -0,0 +1,20 @@
const Joi = require('joi');
module.exports = (req, res, next) => {
const schema = Joi.object().keys({
title: Joi.string().min(2).required(),
text: Joi.string().min(2).required(),
});
Joi.validate({
title: req.body.title,
text: req.body.text,
},
schema, (validateErr) => {
if (validateErr) {
return next({ status: 400, message: 'Form is invalid.', error: validateErr.details });
}
return next();
});
};

View File

@ -0,0 +1,20 @@
const Joi = require('joi');
module.exports = (req, res, next) => {
const schema = Joi.object().keys({
title: Joi.string().min(2),
text: Joi.string().min(2),
});
Joi.validate({
title: req.body.title,
text: req.body.text,
},
schema, (validateErr) => {
if (validateErr) {
return next({ status: 400, message: 'Form is invalid.', error: validateErr.details });
}
return next();
});
};

View File

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