{{#if currentUser}} issue

Hi, I found out that many newbies have the same problem as me and I can’t find solution for this.

If I login and refresh the page, register and login forms are loaded before home.

{{#if currentUser}}
{{> home}}
{{else}}
{{> register}}
{{> login}}
{{/if}}

You can see full code here https://www.tutorialspoint.com/meteor/meteor_accounts.htm

I have tried many other tutorials and all have the same problem.

Is there a solution for this, with example?

{{currentUser}} is there as soon as the user is logged in AND the user data (profile, email, etc.) is loaded.
When you refresh the page, the data is not there, so currentUser is null for a short time.

You can fix this by using {{ loggingIn }} additionaly which is true while login is still processing.

so show some kind of loader while loggingIn is true and currentUser is still null

1 Like

look at iron router with waiton data function

It works if you make it like this:

{{#if loggingIn}}
Loading…
{{else}}
{{#if currentUser}}
{{> home}}
{{else}}
{{> register}}
{{> login}}
{{/if}}
{{/if}}

For waiton, I found this:

Meteor.publish(‘user-profile’, function() {
return Meteor.users.find({_id : this.userId}, {fields : {profile : 1}});
});

Router.route(’/’, {
name: ‘posts’,
template: ‘posts’,
waitOn: function () {
return [Meteor.subscribe(‘user-profile’)];
}
});

This only works, if I add {{#if currentUser}} in content. In navigation still renders content for not loggedin users first and then content for loggedin.

So complicated, for beginners :sweat: