Iron router restrict admin access with hooks
I have a problem with a hardware router.
when I first load the page / admin / deps then the route works fine. Then I go to any other page by following the link and then back to / admin / deps / at that time in the console I get "Route dispatch was never displayed". Did you forget to call this.next () in onBeforeAction? "and the template does not render
BUT. If I change something in the code using the hot-swap code, the meteorite reloads the page, in which 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 the console screen when the route is not working as expected
Here's the 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);
}
});
source to share
Can you fit your code into classic Javascript syntax? Almost all of the documents are not in the language you provide, which does not make the support you can get any easier.
Also, I would say that your onBeforeAction of your @AdminRouteController class might be a bug. Try slicing the if / elseif / else statement in several ways, for example:
class @AdminRouteController extends RouteController
onBeforeAction: ->
console.log "onBeforeAction: #{@url}"
if Meteor.loggingIn()
@render 'loading'
else
@next()
if !Meteor.userId() or !Meteor.user().hasAccess 'admin'
@render 'notFound'
else
@next()
Hope this helps, but I would be more helpful with the classic tbh javascript syntax.
EDIT
thanks for posting your code in .js. Usually when you get the error "Submit route was never displayed. Did you forget to call this.next () in onBeforeAction?" because you forgot to call this.next () in onBeforeAction () or onRun () somewhere. I need to figure out where ...
Greetings,
source to share