blaineh
blaineh

Reputation: 2323

Iron Router onBeforeAction isn't being called

I have a /user route set up, which is supposed to render the login template if the current user isn't logged in. The entire router has a waitOn that waits for the currentUser subscription to finish. The problem is that when I go to /user it simply renders the dataNotFound template instead.

Here's the snippets of code that are relevant to this situation. I've been careful to show you them in the order they're defined in my lib/router.js file.

Router.plugin('dataNotFound', {notFoundTemplate: 'notFound'});

Router.onBeforeAction(function () {
    console.log(Meteor.userId())
    if (!Meteor.userId()) this.render('login');
    else this.next();
}, { only: ['user'] });

Router.configure({
    waitOn: function () { return Meteor.subscribe('currentUser'); }
});

Router.route('/user', {
    name: 'user',
    template: 'userView',
    data: function () { return Meteor.user(); }
});

That console.log above doesn't even ever fire. It seems to me that since it should be a reactive function that even if initially the dataNotFound is rendered, then soon after that the onBeforeAction should be fired and render the login template, right?

Upvotes: 2

Views: 1556

Answers (5)

Rohith
Rohith

Reputation: 1321

Put an if statement inside the waitOn function.

waitOn: function () {
  if(Meteor.userId())
    return Meteor.subscribe('currentUser');
}

Refer this comment to know why this is happening: https://github.com/iron-meteor/iron-router/issues/1010#issuecomment-72210587

Upvotes: 0

Kostanos
Kostanos

Reputation: 10404

I found the issue here.

According to this post, thanks to Steeve Cannon. The problem is on waitOn function.

Probably when you logginOut the subscribe to currentUser will fail, and this will make your app in infinite waiting. onBeforeAction runs after waitOn

You will need to check all variables in you Publish to insure waitOn complete successfully.

Upvotes: 0

yoK0
yoK0

Reputation: 325

I had a problem with onBeforeAction hook after upgrading from meteor 0.9.1 to 1. It didnt get fired when it should. Like after I log out, I enter address manually and instead of login page I can see the actual site waiting for data that never comes. onRun hook solved it, but as docs says it gets fired only once.

Router.onRun(function () {

    if (!Meteor.userId()) {
       this.render('login'); 
    } else { 
       this.next();
    }
}, { only: ['user'] });

Upvotes: 1

Joel Tadmor
Joel Tadmor

Reputation: 121

It's very bizarre that your console log doesn't even fire. I have a few ideas, but first want to address the last piece of your question.

The dataNotFound plugin is triggered when the data function fires on your rout. This means it is bypassing your onBeforeAction hook altogether, and not that it isn't getting there.

One thing I can think of that might be worth trying would be wrapping the 'user' route action in a if ( this.ready() ) statement:

edit:

Router.route('user', { // Your other stuff action: function() { if this.ready() { this.render(); },

The reason I suggest this is just that you are using a waitOn statement, but I'm not 100% sure how that works if you don't have a this.ready() somewhere in your route, because that's what (again, from my reading of the documentation, have not fiddled around with it) tells the router what to wait before executing. Possibly it's not waiting at all right now.

Upvotes: 1

Ryan Glover
Ryan Glover

Reputation: 121

Try swapping out Meteor.userId() with Meteor.user() in your if statement. See this link for reference on how to handle checking for the user in a before filter: http://www.manuel-schoebel.com/blog/meteorjs-iron-router-filters-before-and-after-hooks.

Upvotes: 0

Related Questions