Meteor 流星角色和铁路由器配合得好吗?

Meteor 流星角色和铁路由器配合得好吗?,meteor,roles,iron-router,Meteor,Roles,Iron Router,我有一个Meteor应用程序,它有一个编辑器页面,只有编辑器才能访问。我正在使用Iron Router,我的Router.map如下所示。然而,这并不是一种奇怪的方式。如果我提供了一个指向编辑器页面的链接,那么一切都很好,但是如果我尝试输入/editor url,那么它总是重定向到home,即使用户角色设置正确 (我排除的一件事是,如果Meteor.userId()不是在Roles.userIsInRole调用之前设置的。) 有人知道为什么会这样吗 Router.map(function() {

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

(我排除的一件事是,如果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');
          }
        }
      });
      ...
});

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

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

例如:


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

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

例如:


谢谢你,内森。雷莫达尔是什么?哎呀,我把它拿走了
Remodal
是一个定制的反应式模态软件包。谢谢Nathan。雷莫达尔是什么?哎呀,我把它拿走了
Remodal
是一个定制的反应模式包。
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']
  });
}