passport.js成功验证没有调用next()

时间:2013-10-02 05:59:37

标签: javascript node.js authentication passport.js

我使用passport.js进行身份验证。看来,在成功验证后,护照不会拨打next(),因此我的Express路线永远不会被执行。我看到了这两种我已经实现的身份验证策略的行为。

app.js

app.use(passport.initialize());
passport.use(authentication.local);
passport.use(authentication.bearer);

routes.setup(app, passport);

routes.js

function setup (app, passport) {
  // routes.authentication.token not called, despite successful authentication.
  app.post('/authentication/token', passport.authenticate('local'), 
      routes.authentication.token);
}

authentication.js

var local = new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password'
  },

  function (email, password, done) {
    var user = new User({ email: email });

    user.fetch({
      success: function (user) {
        if (bcrypt.compareSync(password, user.get('encryptedPassword'))) {
          // This line shows in the console, but the route after is never executed.
          console.log("LocalStrategy success!");
          done(null, user); // Tell passport we have success.
        } else {
          done(null, false, { message: 'The password given was incorrect.' });
        }
      },

      error: function (err) {
        done(null, false, { message: 'The email address (' + email + ') does not belong to any user.' });
      }
    });
  }
);

更新

这是一个问题的单文件复制,可能比上面不完整的多文件问题更容易理解:

// ----------------------------------------------------------------------------
// Environment/Configuration
// ----------------------------------------------------------------------------
var config =  require('./lib/config');

var environment = process.env.NODE_ENV || 'development';
config.load(__dirname + '/config/environment', environment);
config.loadDev(__dirname + '/config/environment', environment);

// ----------------------------------------------------------------------------
// Requirements
// ----------------------------------------------------------------------------
var express = require('express')
  , passport = require('passport')
  , bcrypt = require('bcrypt')
  , LocalStrategy = require('passport-local').Strategy
  , User = require('./app/models/user');


// ----------------------------------------------------------------------------
// HTTP Server
// ----------------------------------------------------------------------------
var app = express();

// ----------------------------------------------------------------------------
// Middleware
// ----------------------------------------------------------------------------
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);

app.use(passport.initialize());

passport.use(new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password'
  },

  function (email, password, done) {
    console.log("Authenticating user.", email, password);
    var user = new User({ email: email });

    user.fetch({
      success: function (user) {
        if (bcrypt.compareSync(password, user.get('encryptedPassword'))) {
          // This log statement shows in the console.
          console.log("LocalStrategy success!");
          done(null, user);
        } else {
          done(null, false, { message: 'The password given was incorrect.' });
        }
      },

      error: function (err) {
        console.log('LocalStrategy failure!');
        done(null, false, { message: 'The email address (' + email + ') does not belong to any user.' });
      },

      complete: function (user, res) {
        console.log(res);
      }
    });
  }
));

app.post('/authentication/token', passport.authenticate('local'), function (req, res, next) {
  // This line is never reached.
  console.log('I\'m authenticated!');
});

// ----------------------------------------------------------------------------
// Boot
// ----------------------------------------------------------------------------
app.listen(process.env.PORT);
console.log('Server started on port: %d', process.env.PORT);

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我不小心从serializeUser函数中删除了回调。

也许检查序列化和反序列化功能是否正确。

以下代码是我使用的代码,您的代码可能会有所不同,具体取决于您是使用用户的ID进行序列化还是使用其他字段。

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

passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user) {
        done(err, user);
    });
});

答案 1 :(得分:0)

问题似乎是Express中间件的排序。 app.use(passport.initialize());和护照设置需要在 app.use(app.router);

之前