2

我有一个带有编辑器页面的 Meteor 应用程序,只有编辑器才能访问。我正在使用 Iron-Router,我的 Router.map 如下所示。然而,这并不是以一种奇怪的方式工作。如果我提供了编辑器页面的链接,那么一切都很好,但是如果我尝试输入 /editor url,那么即使用户角色设置正确,它也会始终重定向到主页。

(我排除的一件事是如果 Meteor.userId() 在之前调用 Roles.userIsInRole 之前没有设置。)

有谁知道为什么会这样?

Router.map(function() {
      ...
      this.route('editor', {
        path: '/editor',
        waitOn: function() {
          //handle subscriptions
        },
        data: function() {
          //handle data
        },
        before: function() {
          if ( !Roles.userIsInRole(Meteor.userId(), 'editor') ) {
            this.redirect('home');
          }
        }
      });
      ...
});
4

1 回答 1

4

Roles包设置了一个自动发布,用于发送集合roles上的属性Meteor.users。不幸的是,您无法获得自动发布的订阅句柄,因此您需要自己制作。

设置一个发布用户所需数据的新订阅,然后配置路由器以在显示任何页面之前检查数据是否准备就绪。

例如:

if (Meteor.isServer) {
  Meteor.publish("user", function() {
    return Meteor.users.find({
      _id: this.userId
    }, {
      fields: {
        roles: true
      }
    });
  });
}

if (Meteor.isClient) {
  var userData = Meteor.subscribe("user");
  Router.before(function() {
    if (Meteor.userId() == null) {
      this.redirect('login');
      return;
    }
    if (!userData.ready()) {
      this.render('logingInLoading');
      this.stop();
      return;
    }
    this.next(); // Needed for iron:router v1+
  }, {
    // be sure to exclude the pages where you don't want this check!
    except: ['register', 'login', 'reset-password']
  });
}
于 2014-02-23T02:13:20.457 回答