jaredhanson / passport-local

Username and password authentication strategy for Passport and Node.js.
https://www.passportjs.org/packages/passport-local/?utm_source=github&utm_medium=referral&utm_campaign=passport-local&utm_content=about
MIT License
2.74k stars 498 forks source link

failureFlash - undefined is not a function #99

Closed gregordotjs closed 9 years ago

gregordotjs commented 9 years ago

Always when I try to use failureFlash, I get an "undefined is not a function" error. Otherwise, if I omit the property, it works fine.

Here's the source code:

var passport = require('passport')
  , LocalStrategy = require('passport-local').Strategy;

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

passport.use(new LocalStrategy(function(username, password, done) {

    new User().where({username: username, password: password}).fetch({require: true})
    .then(function(user){
        return done(null, user.toJSON());
    }).catch(function(error){
        return done(null, false, { message: 'Invalid username or password.' });
    });

  }
));

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  new User({id: id}).fetch().then(function(user){
    done(null, user.toJSON());
  });
});

app.post('/login', passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login', failureFlash: true }) );

And here's the error:

TypeError: undefined is not a function at allFailed (C:\some_folder\some_project\node_modules\passport\lib\middleware\authenticate.js:111:15) at attempt (C:\some_folder\some_project\node_modules\passport\lib\middleware\authenticate.js:160:28) at Strategy.strategy.fail (C:\some_folder\some_project\node_modules\passport\lib\middleware\authenticate.js:277:9) at Strategy.authenticate (C:\some_folder\some_project\node_modules\passport-local\lib\strategy.js:75:17) at attempt (C:\some_folder\some_project\node_modules\passport\lib\middleware\authenticate.js:341:16) at authenticate (C:\some_folder\some_project\node_modules\passport\lib\middleware\authenticate.js:342:7) at Layer.handle as handle_request at next (C:\some_folder\some_project\node_modules\express\lib\router\route.js:110:13) at Route.dispatch (C:\some_folder\some_project\node_modules\express\lib\router\route.js:91:3) at Layer.handle as handle_request at C:\some_folder\some_project\node_modules\express\lib\router\index.js:267:22 at Function.proto.process_params (C:\some_folder\some_project\node_modules\express\lib\router\index.js:321:12) at next (C:\some_folder\some_project\node_modules\express\lib\router\index.js:261:10) at SessionStrategy.strategy.pass (C:\some_folder\some_project\node_modules\passport\lib\middleware\authenticate.js:318:9) at SessionStrategy.authenticate (C:\some_folder\some_project\node_modules\passport\lib\strategies\session.js:67:10) at attempt (C:\some_folder\some_project\node_modules\passport\lib\middleware\authenticate.js:341:16) at authenticate (C:\some_folder\some_project\node_modules\passport\lib\middleware\authenticate.js:342:7) at Layer.handle as handle_request at trim_prefix (C:\some_folder\some_project\node_modules\express\lib\router\index.js:302:13) at C:\some_folder\some_project\node_modules\express\lib\router\index.js:270:7 at Function.proto.process_params (C:\some_folder\some_project\node_modules\express\lib\router\index.js:321:12) at next (C:\some_folder\some_project\node_modules\express\lib\router\index.js:261:10)

irrisor commented 9 years ago

Reading up on "Flash Messages" here indicates that you need connect-flash when using express 3.x.

gregordotjs commented 9 years ago

whoa, I completely missed this. Of course, now it works fine. I had to add req.flash for render, like this:

res.render('login',  { message: req.flash('error') });

thanks a lot!