Meteor:我如何才允许用户访问页面?

时间:2015-11-13 18:51:23

标签: meteor

我正在使用铁路由器,我有一个特殊类型的帐户(Meteor.user()。profile中的某些值)我想知道是否有使用铁路由器的方法,而不是必须设置限制在每一条路线(家庭。登录等除外),我只能允许他们一页?

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:0)

查看此页面... http://www.manuel-schoebel.com/blog/meteorjs-iron-router-filters-before-and-after-hooks

基本上,您可以设置将在每个路由之前运行的全局onBeforeAction挂钩,然后在需要时将用户重定向到默认路由。您也可以使用之前的特定路线,这里是页面中的示例...

////////////////
// BeforeHooks
////////////////
// I use an object that contains all before hooks
var IR_BeforeHooks = {
    isLoggedIn: function(pause) {
        if (!(Meteor.loggingIn() || Meteor.user())) {
          Notify.setError(__('Please login.'));
          this.render('login');
          pause();
        }
    },
    somethingForAnyRoute: function() { ... },
    ...
    // add more before hooks here
}

// (Global) Before hooks for any route
Router.onBeforeAction(IR_BeforeHooks.somethingForAnyRoute);
...

// Before hooks for specific routes
// Must be equal to the route names of the Iron Router route map
Router.before(IR_BeforeHooks.isLoggedIn, {only: ['userAreaA', 'userAreaB']});

此外,从Meteor.user()。profile中触发路由保护可能是一个坏主意,因为用户可以从客户端控制台访问和更改配置文件中的任何内容。这应该直接在Meteor.user()对象中进行安全性,然后在服务器端查看。我不是这方面的专家,因此如果担心安全问题,可能需要更多的研究。