Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uploading files with multer after user is authenticated

using multer, I can successfully upload any type of file to my node.js backend from my reactjs client and I can create a user as well. Now I am trying to figure out a way to only allow authenticated users to upload files. Does anyone know of any resources that can help me figure out what I need to do?

like image 384
Antonio Keo Avatar asked Nov 01 '25 08:11

Antonio Keo


1 Answers

you can use any authentication middleware on your upload route. for example.

 const upload = multer({ dest: 'uploads/' })

   const authentication = function (req, res, next) {
   // handle authentication
    if(authenticated) return next()
    next({error})
   })


   app.post('/profile', authentication, upload.single('avatar'),
   function (req, res, next) {

   })

this will authenticate requests before multer handles the request

like image 105
Wisdom Ekeh Avatar answered Nov 03 '25 21:11

Wisdom Ekeh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!