Moro Silverio
Moro Silverio

Reputation: 97

iron:router redirect not admin user

I want to prevent some users to enter specific routes. I'va seen examples for not logged users:

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

But when I try

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

I receive the next message

iron_core.js?hash Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?

Upvotes: 0

Views: 116

Answers (2)

Luna
Luna

Reputation: 1178

Here you go:

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');
    }
});

Upvotes: 0

Moro Silverio
Moro Silverio

Reputation: 97

Tried something like this and worked

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

Upvotes: 0

Related Questions