mcek mcek -4 years ago 77
Node.js Question

bookshelf.js doesnt work with passport

I hava a simple auth panel with bookshelf and passport, but when i try log in i get white empty page 500. I try all solutions which i found in web. Please help.

config/passport.js

...
passport.use('local-login', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password, done) {
User.where({username: username}).fetch().then(function(err, user) {
if (err) return done(err);
if (!user) {
return done(null, false, req.flash('loginMessage', 'Cant find user!'));
}
user = user.toJSON();
if(User.validPassword(password, user.password)){
return done(null, false, req.flash('loginMessage', 'Invalid pass!'));
}
return done(null, user);
}).catch(function(err) {
console.error(err);
});
}
));
...


routes/auth.js

...
router.post('/login', passport.authenticate('local-login', {
successRedirect: '/users/profile',
failureRedirect: '/auth/login',
failureFlash: true
}));
...


server.js

const express = require('express');
const session = require('express-session');
const passport = require('passport');
const flash = require('connect-flash');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');

const config = require('./config/app.js');

const index = require('./routes/index');
const auth = require('./routes/auth');
const users = require('./routes/users');

require('./config/passport.js')(passport);

const app = express();

app.set('env', config.website.env);

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')))
app.use(session({
secret: config.security.salt,
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

app.use('/', index);
app.use('/auth', auth);
...

Answer Source

I think the major issue here is in the use of then(). It does not take an err argument as traditional callbacks.

Another minor issue is that the catch() should forward the error to passport instead of just logging it.

So try changing it to something like

passport.use('local-login', new LocalStrategy({
    passReqToCallback : true
  },
  function(req, username, password, done) {
    new User({username: username})
      .fetch()
      .then(function(user) {
         if (!user) {
           return done(null, false,
             req.flash('loginMessage', 'Cant find user!'));
         }
         user = user.toJSON();
         if (User.validPassword(password, user.password)) {
           return done(null, false,
             req.flash('loginMessage', 'Invalid pass!'));
         }
         return done(null, user);
      })
      .catch(function(err) {
         return done(err);
      });
    }
  ));

As a side note I was a bit confused by User.validPassword(). Does it return a true value for invalid passwords?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download