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

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


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

最佳答案

我认为这里的主要问题是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()有点困惑。它是否为无效密码返回真实值?

07-28 09:01