bookshelf.js不适用于护照

时间:2017-02-28 11:13:57

标签: node.js promise passport.js bookshelf.js

我有一个带书架和护照的简单验证面板,但是当我尝试登录时,我得到空白页500.我尝试了我在网上找到的所有解决方案。请帮忙。

配置/ 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);
            });
        }
    ));
...

路由/ 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);
...

1 个答案:

答案 0 :(得分:0)

我认为这里的主要问题是使用then()。它不会将err参数作为传统回调。

另一个小问题是catch()应该将错误转发给护照,而不是仅仅记录它。

所以尝试将其更改为

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);
      });
    }
  ));

作为旁注,我对User.validPassword()感到有些困惑。它是否为无效密码返回 true 值?