klanc
klanc

Reputation: 241

METEOR Iron router redirect when it shouldn't

I have this onBeforeAction hook in which I check if the user is NOT logged in OR NOT in a specific role (in this case employer role) and then I redirect them to a specific page (called verification). I works good upon navigation to that page but when I'm already on the page and hit REFRESH it seems like he PASSES the IF condition and continues with Router.go("verification");

The code:

Router.route("/employer/profile", {
  name:"employerProfile",
  template:"employerProfile",
  layoutTemplate:'employerLayout',

    onBeforeAction:function(){

        var user = Meteor.userId();
        if(!user || !Roles.userIsInRole(user, ['employer'])) { // checking if the user satisfies this condition e.g. if its a "GUEST USER" than he can't access this route
              Router.go("verification");
        }
        else {
          this.next();
        }
        return true;

    },


});

Here is a quick VIDEO DEMO showcasing what happens:

http://screencast.com/t/BEgwpXwqvwt

Anyone has an idea?

Upvotes: 0

Views: 40

Answers (1)

Roman Kolesnikov
Roman Kolesnikov

Reputation: 12147

It looks like on page refresh Meteor subscribes to Roles collection, but hasn't downloaded any data yet, so userIsInRole return false.

I think you either need to wait until Roles subscribtion is ready (and it isn't the best option) or redirect to verification page later, when subscription will be ready. I recommend to use server method for this check. Move your verification code inside server method an use it: Meteor.call('verifyUser', function (error, result) { if (result === false) Router.go("verification"); } );

Upvotes: 1

Related Questions