From e5462f684aa33ddc2e2169147eece29bd38d3ae1 Mon Sep 17 00:00:00 2001 From: Mateja Date: Sun, 11 Jul 2021 15:48:02 +0200 Subject: Saving images using Multer. --- server/lib/middleware.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 server/lib/middleware.js (limited to 'server/lib') diff --git a/server/lib/middleware.js b/server/lib/middleware.js new file mode 100644 index 0000000..735fddb --- /dev/null +++ b/server/lib/middleware.js @@ -0,0 +1,31 @@ +const passport = require('passport'); + +const multer = require('multer'); +const uuid = require('uuid'); +const path = require('path'); + + +const isAuth = passport.authenticate('jwt', {session: false}); +const isAdmin = (req, res, next) => { + if (!req.user.admin) + res.status(401).json({status: "You need to be an administrator!"}); + else next(); +} + + +const multerStorage = multer.diskStorage({ + destination: (req, file, cb) => { + cb(null, "uploads/"); + }, + filename: (req, file, cb) => { + cb(null, uuid.v4() + path.extname(file.originalname)); + } +}); +const upload = multer({storage: multerStorage}); + + +module.exports = { + isAuth, + isAdmin, + upload +}; -- cgit v1.2.3