使用带有Sailsjs的本地护照时无响应

时间:2014-02-25 09:22:11

标签: node.js sails.js passport.js

我正在尝试使用sailsjs实现本地护照,并且在尝试进行身份验证时我没有得到任何响应。这是代码:

//config/passport.js

var  passport = require('passport')
    , LocalStrategy = require('passport-local').Strategy;

module.exports = {
    express: {
        customMiddleware: function(app){
            console.log('Express midleware for passport');
            app.use(passport.initialize());
            app.use(passport.session());
        }
    }
};

passport.serializeUser(function(user, done) {
    done(null, {omar: "omar"});
});

passport.deserializeUser(function(id, done) {

        done(null, {omar: "omar"});

});

passport.use(new LocalStrategy(
    function(username, password, done) {
        // asynchronous verification, for effect...
        process.nextTick(function () {

                return done(null, {user: "omar"});

        });
    }
));

_

//LoginController.js
var passport = require('passport');
var loginController = {


    login: function(req, res){
        //res.json(req.param('username'));
        console.log('loginctrl');

        passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
            function(req, res) {
                console.log('passport auth');
                res.send("success");
            }


    },

  /**
   * Overrides for the settings in `config/controllers.js`
   * (specific to LoginController)
   */
  _config: {}


};


module.exports = loginController;

我正在尝试POST到/ login,但我得到的唯一控制台消息是:

  

快递护照中间件
  loginctrl

含义它没有调用LocalStrategy回调而我得到“0 NO RESPONSE”错误。
那是为什么?

1 个答案:

答案 0 :(得分:0)

您在此处拨打passport.authenticate的方式有问题:

passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
            function(req, res) {
                console.log('passport auth');
                res.send("success");
            }

passport.authenticate返回一个应该在请求/响应时调用的函数。

有关Sails / Passport集成的一些示例,请参阅此处: