Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor takes time to know if there's a {{currentUser}} or not

I've a few code that I want to run only when there's noUser and a few when there's a currentUser.
All these are inside the navigation template. Like so...

   {{#if currentUser}}  
     <li class="nav"><a href="{{pathFor 'create'}}">Post</a>  
     </li>  
     <li class="nav"><a>Ola, {{thisUser}}!</a>  
     </li>  
     <li class="nav"><a href="#" id="logout">Log Out</a>  
     </li>  
   {{/if}}  

   {{#if noUser}}  
      <li class="nav"><a href="{{pathFor 'signup'}}">Sign Up</a>  
      </li>  
      <li class="nav"><a href="{{pathFor 'login'}}">Login</a>  
      </li>  
   {{/if}}  

So the problem is that when there's a currentUser(i.e, I'm logged in) and I refresh the page, the code inside the {{#if noUser}} block shows up first then the {{#if currentUser}} block, while the {{#if noUser}} block was only meant to show up when there is no user.
Here's the helper code for the template..

Template.navigation.helpers({

    thisUser: function () {
            return Meteor.user().username;

    },

    noUser: function () {
        var user = Meteor.user();

        if (!user) {
            return true;
        };
    }


});

Don't know what am I doing wrong here. :(
Please help.

like image 877
metpb Avatar asked Oct 26 '14 17:10

metpb


1 Answers

You should use if else conditions instead of noUser helper. And to prevent showing "noUser" block while logging in you have to use {{ loggingIn }} helper. Something like this:

{{#if loggingIn}}
  <p>Loggin in...</p>
{{else}}
  {{#if currentUser}}  
    <li class="nav"><a href="{{pathFor 'create'}}">Post</a>  
    </li>  
    <li class="nav"><a>Ola, {{thisUser}}!</a>  
    </li>  
    <li class="nav"><a href="#" id="logout">Log Out</a>  
    </li>  
  {{else}}  
    <li class="nav"><a href="{{pathFor 'signup'}}">Sign Up</a>  
    </li>  
    <li class="nav"><a href="{{pathFor 'login'}}">Login</a>  
    </li>  
  {{/if}}
{{/if}}

Because Meteor does not know immediately whether user is logged in or not. Therefore you have to use loggingIn helper.

like image 200
Tomas Hromnik Avatar answered Dec 07 '22 10:12

Tomas Hromnik