2

エディターのみがアクセスできるエディター ページを備えた Meteor アプリを使用しています。私は Iron-Router を使用しており、Router.map は次のようになります。ただし、これは奇妙な方法で機能しているわけではありません。エディター ページへのリンクを提供するとすべて問題ありませんが、/editor の URL を入力しようとすると、ユーザー ロールが正しく設定されていても、常にホームにリダイレクトされます。

(私が除外したことの 1 つは、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を設定します。残念ながら、自動パブリケーションのサブスクリプション ハンドルを取得することはできないため、独自に作成する必要があります。rolesMeteor.users

ユーザーの必要なデータを公開する新しいサブスクリプションを設定し、ページを表示する前にデータの準備が整っていることを確認するように Router を構成します。

例えば:

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 に答える