Passport: Error: passport.initialize() middleware not in use

Created on 11 Aug 2012  路  7Comments  路  Source: jaredhanson/passport

I get this error when I try to authenticate with the google OAuth2 strategy, the relevant code can be found here.

Now, when I navigate to /auth/google, I get properly redirected and my debug shows that I receive the profile. But then, node stops with the error:

DEBUG: /home/scan/JavaScript/ponyfolder/node_modules/mongoose/lib/utils.js:436

DEBUG:         throw err;

DEBUG:         
DEBUG:       ^

DEBUG: Error: passport.initialize() middleware not in use
    at IncomingMessage.req.login.req.logIn (/home/scan/JavaScript/ponyfolder/node_modules/passport/lib/passport/http/request.js:30:30)
    at Context.module.exports.delegate.success (/home/scan/JavaScript/ponyfolder/node_modules/passport/lib/passport/middleware/authenticate.js:174:13)
    at Context.actions.success (/home/scan/JavaScript/ponyfolder/node_modules/passport/lib/passport/context/http/actions.js:21:25)
    at verified (/home/scan/JavaScript/ponyfolder/node_modules/passport-google-oauth/node_modules/passport-oauth/lib/passport-oauth/strategies/oauth2.js:133:18)
    at Promise.app.get.passport.authenticate.scope (/home/scan/JavaScript/ponyfolder/app/auth.coffee:34:16)
    at Promise.addBack (/home/scan/JavaScript/ponyfolder/node_modules/mongoose/lib/promise.js:128:8)
    at Promise.EventEmitter.emit (events.js:88:17)
    at Promise.emit (/home/scan/JavaScript/ponyfolder/node_modules/mongoose/lib/promise.js:66:38)
    at Promise.complete (/home/scan/JavaScript/ponyfolder/node_modules/mongoose/lib/promise.js:77:20)
    at cb (/home/scan/JavaScript/ponyfolder/node_modules/mongoose/lib/query.js:1392:32)

I have no idea where to go from there. First time using Mongoose, so perhaps I did something wrong there.

Using express 3 and mongoose 3.

Most helpful comment

Yes passport initialization is mandatory.

I added
router.use(passport.initialize());

before actually using the code and it worked for me.

All 7 comments

Closing, as I'm unable to reproduce this. Let me know if your still having troubles and can isolate the issue to Passport specifically.

I get this error if I use express-resource to define a REST API before configuration. This question on stackoverflow is related to this issue also: http://stackoverflow.com/questions/10497349/why-does-passport-js-give-me-a-middleware-error (although I am not sure that the answer is correct - my understanding was that app.router is included by default at the end of the stack even if you don't explicitly use it).

Anyhow that is a couple of different possibilities if you are getting this error - try to ensure you are not defining routes via express-resource prior to app.config and that you are using the router ... alhough I'm not sure why either one of these would trigger this and it still seems like a bug, albeit a minor one once you figure it out.

The answer on Stack Overflow is exactly correct. app.router is included at the end of the middleware stack at the time that you declare routes. If you then use() more middleware after that, that middleware comes _after_ app.router. The upshot is that passport.initialize() needs to come before app.router, so its recommended to configure your app before declaring routes.

My understanding was that

app.configure(function() {
    app.use(passport.initialize());
});

and

app.configure(function() {
    app.use(passport.initialize());
    app.use(app.router);
});

Is functionally equivalent, because express implicitly includes it as the last item in the middleware stack. I am going mainly by other questions I have seen on Stack Overflow so I could be incorrect, but it seems that this is logical, and the apps I have coded do seem to work in such a way as to indicate that if I leave out app.router entirely, it positions all of my routes last, after all other middleware. Do you information to the contrary?

In any event, I like passport, particularly the way it is modularized. This is the reason I moved to it from everyauth. However, it was a bit frustrating to find that I could not use express-resource at the same time as I was connecting to my db and defining models, etc. because that was the perfect time to define nested resources that follow the same patterns as the relationships between my models. My passport strategies use verify functions that need the User model to be available for authentication, and if I have a statement like app.resource('users', ) what the module actually does behind the scenes is setup middleware in a similar manner to defining routes. This caused passport to throw the error mentioned in this issue. So now I have another module that goes back and re-visits all my looping through models and relationships to expose them as nested routes via express-resource to avoid this. Passport seems a bit finicky about the order in which you use it - I have gotten this same error in a couple of other scenarios. But maybe this is unavoidable - I have not delved much into the source code for passport - just enough to be able to define my own strategies.

Yes passport initialization is mandatory.

I added
router.use(passport.initialize());

before actually using the code and it worked for me.

What worked for me was putting:

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

before any other app.use.

What worked for me was putting:

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

before any other app.use.

yes, it is wroking.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

puradox picture puradox  路  4Comments

wongeun picture wongeun  路  4Comments

Gibbo3771 picture Gibbo3771  路  4Comments

andrewbanchich picture andrewbanchich  路  4Comments

tobymurray picture tobymurray  路  6Comments