Question

I'm trying the workaround as described here

https://github.com/jaredhanson/passport/issues/14

app.use(passport.initialize());
app.use(passport.session());  
app.use(app.router);
app.use(express.static(__dirname + '/public'));

Works fine

app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(passport.initialize());
app.use(passport.session()); 

gives

DEBUG: Error: passport.initialize() middleware not in use
    at IncomingMessage.<anonymous> (/.../node_modules/passport/lib/passport/http/request.js:30:30)
    at Context.<anonymous> (/.../node_modules/passport/lib/passport/middleware/authenticate.js:92:11)
    at Context.<anonymous> (/.../core/node_modules/passport/lib/passport/context/http/actions.js:21:25)
    at Strategy.success (native)
Was it helpful?

Solution

You still need to app.use(app.router) after Passport. Otherwise, your route will run before any Passport code, and that's why you see the error. This should work:

app.use(express.static(__dirname + '/public'));
app.use(passport.initialize());
app.use(passport.session()); 
app.use(app.router);

OTHER TIPS

Might help someone, I had the same issue. My app configure looked like this. This caused the error.

app.configure(function() {
  ....
app.use(app.router);    
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

});

I had to reorder as below for it to work.

app.configure(function() {
  ....
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);    
app.use(express.static(path.join(__dirname, 'public')));

});

What worked for me was putting:

app.use(passport.initialize());
app.use(passport.session());

before any other app.use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top