Search code examples
meteoriron-router

iron-router restrict admin access with hooks


i have a problem with iron-router.

when I first load the page /admin/deps, then route will work out fine. Then I go to any other page by clicking on the link, and then back to /admin/deps/ in that time in the console I get "Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?" and template is not rendering

BUT .. if I change anything in the code using hot-swap code, a meteor reloads the page and in this case, everything works as expected.

here are my controllers and routes:

class @AdminRouteController extends RouteController
  onBeforeAction: ->
    console.log "onBeforeAction: #{@url}"
    if Meteor.loggingIn()
      @render 'loading'
    else if !Meteor.userId() or !Meteor.user().hasAccess 'admin'
      @render 'notFound'
    else
      @next()

class @AdminPagebleRouteController extends AdminRouteController
  pageable: true 
  perPage: 20    

  limit: (name = null) ->
    Session.get(varName(@, name)) || @perPage

  incLimit: (name = null, inc = null) ->
    inc ||= @perPage
    Session.set varName(@, name), (@limit(name) + inc)

  resetLimit: (name = null) ->
    Session.set varName(@, name), null

  loaded: (name = null) ->
    true

  onRerun: ->
    console.log "onRerun: #{@url}"
    @next()

Router.route '/admin', name: 'admin'
class @AdminController extends AdminRouteController
  template: 'adminHome'
  action: ->
    document.title = i18n 'admin.title'
    super()

Router.route '/admin/deps', name: 'deps'
class @DepsController extends AdminPagebleRouteController
  template: 'deps'
  perPage: 20

  waitOn: ->
    @subscribe 'adminDeps', @limit()

  data: ->
    deps: DepartmentsCollection.find()

  loaded: ->
    @limit() > DepartmentsCollection.find().count()

  action: ->
    document.title = i18n 'deps.title'
    super()

  onRun: ->
    console.log "onRun: #{@url}"
    @resetLimit()
    @next()

  load: ->
    $('html, body').animate({ scrollTop: 0 }, 400)
    $('.content').hide().fadeIn(1000)

and here is console screen when route does not work properly enter image description here

Here is code in javascript:

AdminRouteController = RouteController.extend({
  onBeforeAction: function(){
    console.log("onBeforeAction: " + this.url);
    if(Meteor.loggingIn())
      this.render('loading');
    else if(!Meteor.userId() || !Meteor.user().hasAccess('admin'))
      this.render('notFound');
    else
      this.next();
  }
});

varName = function(inst, name){
  name = name && ("_" + name || "");
  return inst.constructor.name + name + "_limit";
}

AdminPagebleRouteController = AdminRouteController.extend({
  pageable: true,
  perPage: 20,

  limit: function (name){
    return Session.get(varName(this, name)) || this.perPage;
  },
  incLimit: function (name, inc){
    inc = inc ? inc : this.perPage;
    return Session.set(varName(this, name), (this.limit(name) + inc));
  },
  resetLimit: function (name){
    return Session.set(varName(this, name), null);
  },
  loaded: function (name){
    return true;
  },
  onRerun: function (){
    console.log("onRerun: " + this.url);
    this.next();
  }
});

Router.route('/admin', name: 'admin');
AdminController = AdminRouteController.extend({
  template: 'adminHome',
  action: function(){
    document.title = i18n('admin.title');
    super();
  }
});

Router.route('/admin/deps', name: 'deps');
DepsController = AdminPagebleRouteController.extend({
  template: 'deps',
  perPage: 20,

  waitOn: function(){
    this.subscribe('adminDeps', this.limit());
  },
  data: function(){
    return { deps: DepartmentsCollection.find() };
  },
  loaded: function(){
    return this.limit() > DepartmentsCollection.find().count();
  },
  action: function(){
    document.title = i18n('deps.title');
    super();
  },
  onRun: function(){
    console.log("onRun: " + this.url);
    this.resetLimit();
    this.next();
  },
  load: function(){
    $('html, body').animate({ scrollTop: 0 }, 400);
    $('.content').hide().fadeIn(1000);
  }
});

Solution

  • Finally, I've found the answer, problem was in DepsController, in load() function. It fired before onBeforeAction(), and it is also need to call this.next ()