Giter Site home page Giter Site logo

Comments (7)

hailq avatar hailq commented on April 26, 2024 13

But what happen if i need to parser the form data to get the authentication value? What should i do in this case?

from multer.

jpidelatorre avatar jpidelatorre commented on April 26, 2024 5

@lorenzotsi You need to use multer per route instead of globally. (https://github.com/jpfluger/multer/blob/examples/multer-upload-files-to-different-directories.md)

Then you can add an auth middleware before multer like this:

router.post('/uploads', function (req, res, next) {
    if (!authorized) {
        res.send(403);
    } else {
        next();
    }
},
multer({ dest: '/uploads/' }),
function (req, res, next) {
   // whatever you were planning to do.
});

I know it's late, but I hope it ends up helping someone.

from multer.

hacksparrow avatar hacksparrow commented on April 26, 2024

Load the auth middleware before multer.

from multer.

lorenzotsi avatar lorenzotsi commented on April 26, 2024
...
// Multipart file upload
app.use(multer(
{
  dest: wwwroot + path.sep + 'uploaded' + path.sep, 
  onFileUploadStart: function (file) {
    //TODO : apply security check : user auth, file size, number...
    console.log(file.fieldname + ' is starting ...')
},
onFileUploadComplete: function (file) {
    console.log(file.fieldname + ' uploaded to  ' + file.path)
}
}));
...
app.use(passport.auth.initialize());
app.use(passport.auth.session());

I've tried to put the last 2 lines before multer init, but no way. Is it some other middleware for security you're referring to? Or Is there a way to access request object inside onFileUploadStart()? In that case I would be able to check the user.

Thanks

from multer.

lorenzotsi avatar lorenzotsi commented on April 26, 2024

@hacksparrow Could you give me advice, please? I can't catch if it is a limit of express, of multer , both, a versions uncompliancy or myself in making mistake with the config, Thanks

from multer.

sigurdga avatar sigurdga commented on April 26, 2024

@lorenzotsi You can do the auth check on the route that receives the files. I use passportjs and get a req.user I can do checks on.

from multer.

lorenzotsi avatar lorenzotsi commented on April 26, 2024

@sigurdga Thanks for your support, but that can be applied after. I mean, first the file is uploaded (that is what I'm trying to prevent), in the route I could then cancel the just uploaded file in case user not logged in. I hoped in a better solution.

from multer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.