使用sails作为REST API进行护照js集成

时间:2015-06-29 16:10:25

标签: mysql node.js sails.js passport.js passport-local

我正在尝试使用护照js在我的本地mysql数据库中进行身份验证。我正在使用postman扩展来测试应用程序。 我发送两个字段,即用户名和密码进行身份验证。当任何一个字段为空时,响应以json格式显示为

{
  "message": "Missing credentials",
  "user": false
}

但是当我传递两个字段的值时,我得到500个内部服务器错误。

  

错误:发送500(“服务器错误”)响应:    TypeError:无法读取未定义的属性“message”       在d:\ Test \ api \ controllers \ AuthController.js:25:23       在Strategy.strategy.error(d:\ Test \ node_modules \ passport \ lib \ middleware \ authenticate.js:333:18)       在Strategy.authenticate(d:\ Test \ node_modules \ passport-local \ lib \ strategy.js:94:17)       在尝试时(d:\ Test \ node_modules \ passport \ lib \ middleware \ authenticate.js:341:16)       在authenticate(d:\ Test \ node_modules \ passport \ lib \ middleware \ authenticate.js:342:7)       在Object.module.exports.login(d:\ Test \ api \ controllers \ AuthController.js:37:7)       at bound(C:\ Users * \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ lodash \ dist \ lodash.js:729:21)       在routeTargetFnWrapper(C:\ Users * \ AppData \ Roaming \ npm \ node_modules \ sails \ lib \ router \ bind.js:179:5)       在回调(C:\ Users * \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ express \ lib \ router \ index.js:164:37)       在param(C:\ Users * \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ express \ lib \ router \ index.js:138:11)       传递(C:\ Users * \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ express \ lib \ router \ index.js:145:5)       at nextRoute(C:\ Users * \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ express \ lib \ router \ index.js:100:7)       在回调(C:\ Users * \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ express \ lib \ router \ index.js:167:11)       在C:\ Users * \ AppData \ Roaming \ npm \ node_modules \ sails \ lib \ router \ bind.js:187:7       at alwaysAllow(C:\ Users * \ AppData \ Roaming \ npm \ node_modules \ sails \ lib \ hooks \ policies \ index.js:207:11)       at routeTargetFnWrapper(C:\ Users * \ AppData \ Roaming \ npm \ node_modules \ sails \ lib \ router \ bind.js:179:5)[TypeError:无法读取未定义的属性'message'] **

以下是我的AuthController

var passport=require('passport');

  login:function(req,res){

    passport.authenticate('local', function(err, user, info) {

      if ((err) || (!user)) {

        return res.send({
          message:info.message,
          user: user
        });
      }
      req.logIn(user, function(err) {
        if (err) res.send(err);
        return res.send({
          message:"User Loged In",//info.message,
          user: user
        });
      });

    })(req, res);

  }
};

我使用以下模型进行测试

module.exports = {

  tableName: 'users',
  connection:'TestDB',
  autoCreatedAt:false,
  autoUpdatedAt:false,

  attributes: {
    username:{
        type:'string',
        required:true
      },
    password:{
      type:'string',
      required:true
    },
    toJSON: function() {
      var obj = this.toObject();
      delete obj.password;
      return obj;
    }
  }
};

包含用户名和密码的表格还包含其他字段,例如country。有什么方法可以用护照验证它。

1 个答案:

答案 0 :(得分:0)

您是否已执行此link

的第六步
module.exports.http = {
  middleware: {
    passportInit    : require('passport').initialize(),
    passportSession : require('passport').session(),

    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'passportInit',
      'passportSession',
      'myRequestLogger',
      'bodyParser',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ],
  }
};

也许您希望看到sails-hook-sanpassport,简单快捷