2

How can I determine layout depending on whether the user is logged in or not in Meteor?

I have tried

Router.map(function () {
  this.configure({
    layoutTemplate: Meteor.userId() ? "appLayout" : "startLayout",
  });
});

I get the error

Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
Jamgreen
  • 10,329
  • 29
  • 113
  • 224

3 Answers3

0

It seems that Meteor.user() returns undefined if it's loading and null if it's logged out, same is true of Meteor.userId().

Ryan Taylor
  • 12,559
  • 2
  • 39
  • 34
0
 if(Meteor.userId(){
    layoutTemplate: "appLayout";
 }else{
    layoutTemplate: "startLayout";
 }

Did you try something like this ?

myput
  • 422
  • 1
  • 10
  • 26
-2

You can use this.userId, as error mentioned Meteor.user() you can use in functions, otherwise use Meteor.users.findOne({_id: this.userId}) if you need any data from user collection.

Also, your iron-router structure is deprecated, here are new docs

sdooo
  • 1,851
  • 13
  • 20
  • 1
    `this.userId` is `undefined` even if I'm logged in. Is it my mapping or route structure which is deprecated? According to http://eventedmind.github.io/iron-router/#creating-route-controllers the structure is still used. I don't know how to name each route if I'm using the other structure. – Jamgreen Dec 25 '14 at 17:06
  • Controllers stay the same, but there is no map function anymore, and from what I know no functions at all for that matter(waitOn etc.), http://eventedmind.github.io/iron-router/#route-parameters for creating each route and http://eventedmind.github.io/iron-router/#named-routes for naming them – sdooo Dec 25 '14 at 17:11
  • this.userId MIGHT be undefined because routes are created before Meteor.user() object, therefore this.userId is called before page even loads. Maybe try wrapping it up into function? – sdooo Dec 25 '14 at 17:14