Meteor.user() and Meteor.userId() is not being updated in the client at the same time

We encountered a race condition in our app wherein we got the following client errors (from react) from production

Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

After our investigation, we realized this is happening randomly during logout and the culprit of the loop is that some of our client pages are checking if the user is logged in through Meteor.user(), and if not, will redirect the user to our login page. Our login page checks if the user is already logged in through Meteor.userId() and will redirect the user back to the destination page if it detected the original page the user was trying to access.

Further investigations resulted that randomly, during logout, Meteor.user() is being unassigned first than Meteor.userId() and thus creating the infinite loop. As a quick solution, we aligned our checking using Meteor.userId() for all the redirects.

But nevertheless, I believe that Meteor.user() and Meteor.userId() should be changing at the same time. Is this due to both functions are reactive sources?

1 Like

Very interesting observation, that explains some of the strange behavior I’ve seen in our log files as well.

Will change our code to use Meteor.userId() as well. At least it’s clear why we get null responses on Meteor.user().

Found an undocumented function: Meteor.loggingOut()