user1855596
user1855596

Reputation: 51

iron-router except fails?

So i'm just getting started with iron-router, and I've been building a login system. It works via a .onBeforeAction hook before every route, checking if the user is logged in. However, there are a few routes I want public, so I've added an except option, as per the docs. Except the problem is it doesn't work :( can anybody see why?

Router.route('/new', function () {
  name: 'new',
  this.render('newComp');
});

Router.route('/c/:_id', { 
  name: 'compPage',
    data: function() { return Comps.findOne(this.params._id); }
});


Router.route('/c/:_id/embed', function () {
  name: 'embed',
  this.layout('empty'),
  this.render('compEmbed', {
    data: function () {
      return Comps.findOne({_id: this.params._id});
    }
  });
});

function loginFunction(){
  // all properties available in the route function
  // are also available here such as this.params

  if (!Meteor.user()) {
    // if the user is not logged in, render the Login template
    if (Meteor.loggingIn()) {
      this.render(this.loadingTemplate);
    } else {
      this.layout('empty');
      this.render('login');
    }
  } else {
    // otherwise don't hold up the rest of hooks or our route/action function
    this.next();
  }
}

Router.onBeforeAction( loginFunction, { 
  except: ['embed'] // this aint working
});

Upvotes: 1

Views: 339

Answers (1)

Mikael Lirbank
Mikael Lirbank

Reputation: 4615

The problem seems to be in your route definition, the name param should be in the third param of Router.route(), like this (so your route actually didn't have a name, thus the except:['route.name'] doesn't work):

Router.route('/c/:_id/embed', function () {
  this.layout('empty'),
  this.render('compEmbed', {
    data: function () {
      return Comps.findOne({_id: this.params._id});
    }
  });
}, {
  name: 'embed',
});

More info about named routes here: http://eventedmind.github.io/iron-router/#named-routes

Upvotes: 6

Related Questions