diff options
author | Mateja <mail@matejamaric.com> | 2021-07-11 15:48:02 +0200 |
---|---|---|
committer | Mateja <mail@matejamaric.com> | 2021-07-11 15:48:02 +0200 |
commit | e5462f684aa33ddc2e2169147eece29bd38d3ae1 (patch) | |
tree | f260a210ae7ae1ef167325237d716c5a64a9ba66 /server/lib | |
parent | 3fbd440fe39a2677a12c234f06aaa1bae6a197a4 (diff) | |
download | mevn-ecommerce-e5462f684aa33ddc2e2169147eece29bd38d3ae1.tar.gz mevn-ecommerce-e5462f684aa33ddc2e2169147eece29bd38d3ae1.zip |
Saving images using Multer.
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/middleware.js | 31 |
1 files changed, 31 insertions, 0 deletions
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 +}; |