title: Nodejs Express 4 Multer | Stop file upload if user not authorized
body: <p>I'm using <code>multer</code> as multipart middelware for express4.</p>
  
  <p>Express is configured to use passport as auth middelware, but I cannot find a way to prevent file upload if the user is not authenticated. </p>
  
  <p>I thought to use onFileUploadStart to reject the file, but I cannot find a link with <strong>"request" object</strong>, with which it would be possible to match the user.</p>
  
  <p><strong>Below code use in configuring express vs multer:</strong></p>
  
  <pre><code>...
  // 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());   
  </code></pre>