jaredhanson / passport

Simple, unobtrusive authentication for Node.js.
https://www.passportjs.org?utm_source=github&utm_medium=referral&utm_campaign=passport&utm_content=about
MIT License
22.99k stars 1.24k forks source link

passport.initialize() middleware not in use eventhough correct order #619

Open marcesengel opened 7 years ago

marcesengel commented 7 years ago

Hi there, I'm getting the passport.initialize() middleware not in use error when trying to use req.login. I made sure, that I'm not adding any routes before initializing passport and also made sure, that I'm adding the session middleware before adding the passport middlewares - but I still get the error.

Node: v.8.8.1 passport: ^0.4.0

Thanks in advance!

Edit: As off the documentation, passport.authenticate(...) is also making use of the middleware - so it seems to work for this case...

marcesengel commented 7 years ago

It turned out to be a problem with me passing wrong arguments, as I directly used req.login in a Promise.then()which resolved with only the user object. Still it's an issue that passport logs this error if you simply pass in wrong arguments, it mislead me quiet a lot. Maybe updating the Docs would help avoiding this kind of problem, too.

rosschapman commented 6 years ago

For the record I found this answer really helpful just now. I had this code (koa-specific but basically ctx is just a wrapper on req/res and other app context):

if (user) {
      // the next line borked things (which, eh, I didn't need anyway, 
      // just left here accidentally during dev)
      ctx.state = { success: true } 
      ctx.login(user);
      ctx.redirect('/dashboard');
    } else {
      ctx.state = { error: 'access denied' };
      return ctx.render('auth/signup');
    }

If you change that bad line to ctx.body it works because Passport seems to expect ctx to be in a certain state before calling .login().