From 6147288db7aaadb88ac50955dc87d3c39ebbf069 Mon Sep 17 00:00:00 2001 From: Haris Razis Date: Wed, 16 Dec 2020 09:20:41 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Add=20middlewares?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/middlewares/middleware.js | 24 ++++++++++++++++++++++++ server/middlewares/validator.js | 0 2 files changed, 24 insertions(+) create mode 100644 server/middlewares/middleware.js create mode 100644 server/middlewares/validator.js diff --git a/server/middlewares/middleware.js b/server/middlewares/middleware.js new file mode 100644 index 0000000..3436510 --- /dev/null +++ b/server/middlewares/middleware.js @@ -0,0 +1,24 @@ +const {validationResult} = require('express-validator'); + +module.exports = { + handleErrors(route) { + return async (req, res, next) => { + const errors = validationResult(req); + if (!errors.isEmpty()) { + req.session.errors = errors.errors; + return res.redirect('/auth/' + route); + } + req.session.errors = {}; + next(); + } + }, + handleErrorsPassport(req, res, route, info) { + req.session.errors = info; + return res.redirect('/auth/' + route); + }, + requireAuth(req, res, next) { + if (!req.isAuthenticated()) + return res.redirect('/auth/login'); + next(); + } +}; \ No newline at end of file diff --git a/server/middlewares/validator.js b/server/middlewares/validator.js new file mode 100644 index 0000000..e69de29