PassportJS 和本地策略

PassportJS and local strategy

我在使用 PassportJS 和本地策略时遇到问题。我正在使用 ExpressJS 4.

我的app.js(减少,没有明确的初始化):

var express                        = require('express'),
    ejs                            = require('ejs'),
    path                           = require('path'),
    expressLayouts                 = require('express-ejs-layouts'),
    session                        = require('express-session'),
    compression                    = require('compression'),

    _                              = require('lodash'),
    fs                             = require('fs-extra'),
    colors                         = require('colors'),
    passport                       = require('passport'),
    flash                          = require('connect-flash'),

    localStrategyMiddleware        = require('./middlewares/localStrategy'),
    logMiddleware                  = require('./middlewares/log'),
    localsMiddleware               = require('./middlewares/locals'),
    securityMiddleware             = require('./middlewares/security');

this.app.use(session({
  secret : 'd4qs45sdq6',
  // name : 'sessionId',
  // proxy: true,
  resave: true,
  saveUninitialized: true
}));

this.app.use(flash());
this.app.use(function(request, response, next) {
  response.locals.flash = {
    info: request.flash('info'),
    success: request.flash('success'),
    warning: request.flash('warning'),
    danger: request.flash('danger')
  };
  next();
});

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

this.app.post('/test', passport.authenticate('local', {
      successRedirect: '/user/success',
      failureRedirect: '/user/failure',
      failureFlash: true,
      successFlash: true
    }), function(request, response) {
  console.log('test');
});

我的攻略(./middlewares/localStrategy):

var passport        = require('passport'),
    LocalStrategy   = require('passport-local').Strategy,
    sequelize       = require('../databases/Database').connexion,
    UserModel       = require('../app/models/UserModel')(sequelize);

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

    console.log('test');

    UserModel.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }

      if (!user) {
        return done(null, false, { message: 'Incorrect username.' });
      }

      if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' });
      }

      return done(null, user);
    });
  }
));

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

passport.deserializeUser(function(id, done) {
  console.log(id);
  UserModel.findById(id, function(err, user) {
    done(err, user);
  });
});

module.exports = passport;

问题:我没有收到任何日志,而且每次都被重定向到 user/failure

编辑 1 :

<form action="/test" method="post">
  <input type="hidden" name="_csrf" value="<%=csrfToken%>">
  <input type="text" name="user[username]" placeholder="username">
  <input type="text" name="user[password]" placeholder="password">
  <input type="submit">
  <input type="reset">
</form>

如果您没有收到任何日志,则表示您的 app.js 文件无法访问 ./middlewares/localStrategy

中的代码

尝试更改

localStrategyMiddleware = require('./middlewares/localStrategy')

简单地

require('./middlewares/localStrategy')

您还需要更改表单上的名称属性以匹配护照使用的名称属性,我认为默认设置是用户名和密码,因此除非您想在 localStrategy 中指定替代项,否则您应该使用以下内容。

  <input type="text" name="username" placeholder="username">
  <input type="text" name="password" placeholder="password">