Ironrouter - onBeforeAction question

I’m trying to do a check to see if the user has an object on it, if it doesn’t I render a different template.
Just like checking if the user is logged in with Meteor.userId(). Except my problem is that it seems Meteor.user() only returns certain fields. I can’t get it to return the object on the user.

Question, do I have to create a pub/sub just for currentUser or can I change what is returned by Meteor.user().

Secondly, as I’m trying to do this in an Router.onBeforeAction, can I/do I have to put a waitOn in the onBeforeAction somehow? I just want this info for the routing.

lastly, is there a way to do {name: ‘whatever’} inside the onBeforeAction? Instead of the ugly .render … controller etc?

Maybe I’m going about this the wrong way but its proving tough to get this right.

My code looks like so…

Router.route('/', {name: 'home'});

Router.onBeforeAction(function () {
  if (!Meteor.userId()) {
    this.render('login');
  } else if (!Meteor.user().customObject) {
    this.render('add', {
      controller: 'addController'
    });
  } else {
    this.next();
  }
});

You can publish customObject like this:

Meteor.publish(null, function () {
  var userId = this.userId,
      fields = {customObject:1}

  return Meteor.users.find({_id:userId}, {fields: fields})
})

Check this alanning:roles package’s code on github.

2 Likes

That looks interesting, does the client need to subscribe to anything to get this?

No. If you pass null for name, it auto publishes to client.

1 Like

Thanks I didn’t realise this!