Meteor.user () returns undefined after page reload

I want to check if a user is registered via Meteor.user () inside onBeforeAction on my routes. The problem is that after reloading the page, Meteor.user () returns undefined for a split second before it loads.

Here is my config route:

Router.map(function() { this.route('list', { path: '/list', template: 'list', onBeforeAction: function(){ console.log('onBeforeAction'); if(!Meteor.user()){ Router.go('/login'); } this.next(); } }); }); 

I have a lot of google and workarounds with "waitOn" and "return Meteor.user ();" doesn't seem to work in my case. It’s also interesting ... locally it works fine, so I can reload the page and still remain in the list view, but the deployed application on the module acts as described above and redirects to the login page.

Any ideas? Thanks in advance.

+6
source share
1 answer

It is better to use !!Meteor.userId() to check if the user is logged in, because there is no latency while waiting for user data to be downloaded from the server. In fact, the Meteor.user procedure Meteor.user more or less equivalent:

 function () { return Meteor.users.findOne({ _id: Meteor.userId() }); } 

which explains why it can return undefined even if the user is logged in.

+6
source

All Articles