簡體   English   中英

如何通過流星應用程序中的鐵路由器來限制訪問?

[英]How do limiting access through a iron-router in meteor app?

我有鎖定的路由文件,例如:

Router.map(function(){
    this.route('gameSmall', {path: '/'});
    this.route('gameMedium', {path: '/game-medium'});
    this.route('gameLarge', {path: '/game-large'});
});

等等

如果我想限制對某些路徑的訪問(僅針對具有密碼的某些用戶),可以在路由器文件中對其進行配置嗎? 或僅通過模板中的本機js?

Iron Router不支持通過配置文件限制訪問。 相反,您可以在js源代碼中定義訪問權限。 您可以限制全局訪問和按路由訪問。 兩者都使用onBeforeAction事件來評估對路由的訪問。 onBeforeAction接受您在其中編寫訪問規則的回調函數。

全局onBeforeAction事件可能看起來像這樣:

Router.onBeforeAction(function() {
  if (!Meteor.isServer) {
      // Check the user. Whether logged in, but you could check user's roles as well.
      if (!Meteor.userId()) {
          this.render('pageNotFound'); // Current route cancelled -> render another page
      } else {
          this.next(); // Continue with the route -> will render the requested page
      }
  } 
}, 
{
    except: ['gameSmall']
});

請注意第二個參數中的except字段。 它包含一系列要從onBeforeAction中排除的路由,因此始終會呈現這些路由。 only一個相反的字段,包括要由onBeforeAction評估的路由。

另請注意,我使用了模板pageNotFound(404頁面)。 您可以在IR的配置中定義該頁面,如下所示:

Router.configure({
    notFoundTemplate: 'pageNotFound'
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM