2

Okay, banging my head against the wall on this one. I just switched from everyauth to passportjs and everything worked out great, except my implementation of Valums file uploader.

You can see the Gist of my fileuploader code at https://gist.github.com/4066155

My app.configure for express looks like this:

app.configure( function () {
  app.use(express.methodOverride());
  app.use(express.static(__dirname + '/public'));
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.set('view options', {
    layout: false
  });
  app.use(passport.initialize());
  app.use(passport.session());
});

Strangely enough, if I comment out app.use(passport.session()); - the file uploader seems to work fine. The passport.initialize can stay, as it is not causing any problems.

If I keep the use passport.session in my code, the upload call comes through, it actually does create a temporary file in my tmp directory, but it stays at zero bytes. The server never responds to the web client (no callback) and the file is never actually written by fs.

Even a pointer in the right direction might be very helpful. If you need more context let me know. Thanks.

Update:

If it helps, I diffed the req variable to see the difference when passport.session is not called vs when passport.session is called. The first diff is with passport.session NOT called and the second when it IS called. URL: http://diffchecker.com/Xk8g434Q - line 469 is interesting where it shows the events being bound, and then in the second block of text, events is just {} (empty).

laggingreflex
  • 32,948
  • 35
  • 141
  • 196
Scott Weinert
  • 166
  • 1
  • 7
  • Duplicate question here, with solution: http://stackoverflow.com/questions/14479343/in-node-js-why-does-passport-session-stop-formidable-from-triggering-file-even – Sam Dec 30 '14 at 22:35

2 Answers2

1

The passport.session() method calls your passport.deserializeUser(), which itself usually makes a database call to fetch the user. This database call delays the execution of code that starts listening to the incoming data. That is, the data arrives while nobody is listening for it.

rkusa
  • 4,792
  • 1
  • 22
  • 28
0

If anybody experiences this, one workaround, although not ideal, is to just add an extra layer of middleware around the PassportJS session middleware - it is obviously not going to authenticate the file uploads, but so far nobody has answered my question and I have still been unable to find why it conflicts.

Here's some code you can put in Express's app.configure( function () { just after the app.use(passport.initialize()).

  var sessionMiddleware = passport.session()

  app.use(function(req, res, next) {
    if (req.url.match("/upload")) return next()
    console.log("Using session", req.url)
    sessionMiddleware(req, res, next)
  })
Scott Weinert
  • 166
  • 1
  • 7