Testing for Logged-In User


#1

I’m currently reading & following along with the code in the Discover Meteor book.

I’ve just reached the Creating Posts chapter, and (I like to add my own tweaks here and there - it helps me learn) I added a Spacebars {{#if}}{{/if} block around the link to the New Post form in my header template, so it would only be visible to logged-in users (this may be something that the book will cover eventually anyway – I haven’t read ahead).

The problem is, I get an error from Meteor even though the code looks right to me and uses the right test for a logged-in user (according to the way I understood the Meteor docs, anyhow):

My code:

{{#if Meteor.userId()}}
  <li><a href="{{pathFor 'postSubmit'}}">New post</a></li>
{{/if}}

The error message:

=> Errors prevented startup:
   While building the application:
   client/templates/includes/header.html:21: Expected space
   ...  {{#if Meteor.userId()}}           <li><...
   ^
=> Your application has errors. Waiting for file change.

I’m stuck here, so any help would be greatly appreciated. Thanks!


#2

You can’t just use any old object inside the curly braces.
You need to refer to helpers.
(I don’t remember quite how far into the book you need to get for the chapter on helpers, but you will come across helpers at some point)

There are some global ones that do what you want:
http://docs.meteor.com/#/full/template_currentuser
Like this:

{{#if currentUser}}
  <li><a href="{{pathFor 'postSubmit'}}">New post</a></li>
{{/if}}

#3

Yes, thank you. That’s exactly what I was looking for. I know I’ve seen that statement before, but I couldn’t find it.