How do Meteor.userId(), this.userId, and Meteor.user() work internally?


#1

Can someone either explain the internals of how each of these work or point me to the implemented code?

  • Meteor.userId()
  • this.userId
  • Meteor.user()

Also, what does the this in this.userId refer to?

Edit: Clarified that I want to know the internals of how these work, not what they do.


#2

Meteor.user() - Get the current user record, or null if no user is logged in. A reactive data source.
Meteor.userId() - Get the current user id, or null if no user is logged in. A reactive data source.
this.userId - Access inside the publish function. The id of the logged-in user, or null if no user is logged in. and The id of the user that made this method call, or null if no user was logged in.


#3

Apologies, I should have clarified better. I understand what they do. I want to know how they do it.

Does Meteor.user() call use Meteor.userId() in its function or is it the other way around?
Or are they independent of each other?
What about this.userId and what this refers to?


#4

Ah I see.

Meteor.user() calls Accounts.user() which is found here:

Meteor.userId() calls Accounts.userId() which is found here:

Accounts.user() is implemented here:

Accounts.userId() is implemented here:

this.userId for methods is implemented here:

this.userId for publications is implemented here:

this with regards to a publication is a Subscription object which is implemented here:

this with regards to a method is a DDPCommon.MethodInvocation which is implemented here:


#5

This is something I don’t get. How does userId() return the current userId if its implementation is to throw an error?


#6

The AccountsCommon class contains a userId() function as a placeholder only (an abstract function or method). In the same accounts-base package, you’ll see that both AccountsClient and AccountsServer extend AccountsCommon, and override the userId() function with their own versions. These are the versions that you actually end up calling. You can see the full details in: