FlowRouter中的角色正在发挥作用

时间:2016-01-11 11:17:55

标签: meteor meteor-accounts flow-router

我试图在未经授权的情况下将用户重定向到403页面。我添加了角色' admin','默认组'到CM9Cwq7HXD6yHjKRp,它在模板级别上像魅力一样工作。但是在路由器上没有按预期工作。

我的路线组分为2个主要组

// Public routes
var publicFlowRouter; 
publicFlowRouter  = FlowRouter.group({});

// Private routes
var privateFlowRouter;
privateFlowRouter = FlowRouter.group({
  triggersEnter: [
    function() {

    var route;

    if (!(Meteor.loggingIn() || Meteor.userId())) {

      route = FlowRouter.current();

      if (route.route.name !== 'home') {
        Session.set('redirectAfterLogin', route.path);
      }

      return FlowRouter.go('home');
    }
  }
  ]
});

这些路线没有任何问题,但问题始于adminPrivateFlowRouter;

// Private routes extended for admin
var adminPrivateFlowRouter;
adminPrivateFlowRouter = privateFlowRouter.group({
  triggersEnter: [ 
    function() {
      // If user is not authenticated redirect to homepage
      console.log(Meteor.userId());
      console.log(Roles.userIsInRole(Meteor.userId(), 'admin', 'default-group'));
      if (Roles.userIsInRole(Meteor.userId(), 'admin', 'default-group')) {
        console.log('Authenticated user');       
      } else {
        console.log('403 Access Denied');
        //return FlowRouter.go('home');
      }
    }
  ]
});

工作不稳定。当我刷新同一页面控制台时有时说 CM9Cwq7HXD6yHjKRp 假 403访问被拒绝

CM9Cwq7HXD6yHjKRp 真正 经过身份验证的用户

我无法找到问题所在,谢谢

1 个答案:

答案 0 :(得分:0)

请参阅How to make FlowRouter wait for users collection on the client

FlowRouter.wait()订阅准备就绪时,您可以使用FlowRouter.initialize()Roles手动初始化FlowRouter来解决您的问题。