使用 meteor restivus 时不需要先登录

First Logging in is not required when using meteor restivus

我正在使用 meteor restivus 来休息 api。我遇到的问题是 api 不会强制我登录以发帖并获取 我的代码如下:

Articles = new Mongo.Collection('articles');

if (Meteor.isServer) {

  // Global API configuration
  var Api = new Restivus({
    useDefaultAuth: true,
    authRequired: true,
    prettyJson: true,
 version:'v1'
  });
  
  Api.addCollection(Articles);
}

我做了 POST 使用:

curl -X POST http://localhost:3000/api/v1/articles/ -d "title=Witty Title" -d "author=Jack Rose"

我使用

进行了 GET

curl -X GET http://localhost:3000/api/v1/articles/

但我没有收到强制我先登录才能执行上述 POST 和 GET 操作的错误。我的流星应用程序使用 accounts-password 和 accounts-ui 包。我必须做什么才能让 API 强制我登录,然后才能执行任何 POSTs 或 GET。

我设法解决了这个问题。 authRequired 应该按照下面的代码包含在 addCollection 中:

Articles = new Mongo.Collection('articles');

if (Meteor.isServer) {

  // Global API configuration
  var Api = new Restivus({
    useDefaultAuth: true,
    //authRequired: true,
    prettyJson: true,
    version:'v1'
  });
  
  Api.addCollection(Articles,{
  routeOptions: {
   authRequired: true
  }
  });
}

我有点像个白痴,因为这在文档中。不太清楚,但在文档中 none 较少。希望这对其他人有帮助!

补充一下:

登录后根据文档执行 GET a

curl H "X-Auth-Token: etttttttt-BM2DyXsTe-Gybtttttttttttttttt3Reo" -H "X-User-Id: pwfy4viiiiiiiyz3Kp" http://localhost:3000/api/v1/articles/

然后像这样 POST:

curl -X POST -H "X-Auth-Token: etttttttt-BM2DyXsTe-Gybtttttttttttttttt3Reo" -H "X-User-Id: pwfy4viiiiiiiyz3Kp" http://localhost:3000/api/v1/articles/ -d "title=Witty Title" -d "author=Jack Rose"