2

我正在编写我的应用程序的一部分,它要求用户具有“操作员”角色。我正在 FlowRouter 的triggersEnter函数中检查这一点。我希望向没有操作员角色的用户显示受限访问页面。

我使用 FlowRouter、Roles 和 brettle:accounts-deluxe,它会自动以访客身份登录每个访客。

这是我的代码,routes.js

FlowRouter.route('/switchboard', {
  name: 'switchboard',
  triggersEnter: [function (context, redirect, stop) {
    if (!Roles.userIsInRole(Meteor.userId(), ['operator'])) {
      BlazeLayout.render('main', {
        content: 'restrictedAccess'
      });           
      stop();
    }
  }],
  action: function () {
    BlazeLayout.render('main', {
        content: 'switchboard'
    });
  }
});

本地主机上的一切都按预期工作,但是当应用程序使用mup, 在服务器上运行时triggersEnter,运行时Meteor.user()undefined(Meteor.userId() 返回 ok),并且Roles.userIsInRole调用的结果是false,尽管在数据库中查找很清楚用户具有操作员角色。

我认为在运行 triggersEnter 时用户订阅不可用,这意味着用户集合未在客户端上发布。我有这种感觉,因为如果我通过单击链接访问路由,则 userIsInRole 结果是可以的,但是如果我刷新页面,我会遇到所描述的问题。我想知道为什么这只发生在服务器上,我该如何解决。

4

2 回答 2

1

使用Template.subscriptionsReady标志

<template name="blogPost">
  <a href="/">Back</a>
  {{#if Template.subscriptionsReady}}
    {{#with post}}
      <h3>{{title}}</h3>
      <p>{{content}}</p>
    {{/with}}
  {{else}}
      <p>Loading...</p>
  {{/if}}
</template>

在此处查看完整文档: https ://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management/with-blaze 了解如何处理个人订阅

于 2016-01-12T19:57:51.740 回答
1

原因是 FlowRoutertriggersEnter不会阻止模板渲染,它会在Roles订阅集合之前检查角色。解决方案是FlowRouter.wait()在应用程序初始化上使用,然后为Roles(您需要它是全局的 - 不绑定到模板级别)集合进行全局订阅,并FlowRouter.initialize()在其准备好时调用。

这样,FlowRouter 将等待您的集合,并在准备好检查后进行初始化。

更新

在 localhost 上,本地数据库和应用程序之间的延迟要少得多。部署您的应用程序时,客户端需要更多时间从数据库中获取数据。结果,在 localhost 上,当 FlowRouter 初始化时,您的集合已准备就绪,而在已部署的应用程序上却没有。

于 2016-06-14T14:04:31.017 回答