Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Passport.js give me a middleware error?

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)
like image 557
deltanovember Avatar asked May 08 '12 11:05

deltanovember


1 Answers

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);
like image 189
Linus Thiel Avatar answered Oct 06 '22 23:10

Linus Thiel