2017-04-24 92 views
0

我想阻止某些用戶輸入特定路由。對於未登錄用戶I'va看到的例子:鐵:路由器重定向不是管理員用戶

Router.onBeforeAction 
(
    function() 
    { 
     if (!Meteor.userId()) 
      this.redirect('/'); 
     else 
      this.next(); 
    } 
); 

但是當我嘗試

Router.onBeforeAction 
(
    function() 
    { 
     if (!isadmin(Meteor.userId()) && Router.current().route.getName()=='admin') 
      this.redirect('/'); 
     else 
      this.next(); 
    } 
); 

我收到一條消息

iron_core.js哈希的路由調度從來沒有顯示?您是否忘記了在onBeforeAction中調用this.next() ?

回答

0

嘗試過這樣的事情和工作

Template.admin.onRendered(function() { 
    if (!isadmin(Meteor.userId())) 
    { 
     Router.go('/'); 
    } 
}); 
+0

這是不是做的最好的方式,但它的工作原理 –

0

在這裏你去:

Router.route('/your-url',{ 
    name:'routeName', 
    onBeforeAction:function(){ 
     if(checkSomething){ 
      //check passed, continue routing 
      this.next(); 
     } else { 
      //check failed, redirect. 
      Router.go('/not-authorized'); 
     } 
    }, 

    action: function(){ 
     this.render('templateName'); 
    } 
});