Unified way to access userId

userId can be accessed from everywhere using Meteor.userId(), except:

  1. from publish functions, where you have to use this.userId, and

  2. from methods called from publish functions, where you have to use this.userId (well, I am not sure about this one, but the fact is there is a this.userId in methods, see here).

What is the cleanest way you have found to manage this? Did you find a unified way to declare a function which uses userId and will be used in methods and publish functions, on client and server?

The first thing that jumps to mind is in your method/function you could check to see if this.userId is a function or a string.

I am not sure I got your point. Let’s consider this function:

myFunction = function() {
  // Get the userId
  var userId = ...

  // Do stuff with it
  ...
}

Now suppose you don’t want to pass userId as an argument (because myFunction is nested in several function calls, or is called directly from your template html where it is not easy to pass argument) and myFunction needs to be called from any kind of place (publish functions, methods, events, whatever) on client and server. How do you implement var userId = ...?

I guess I’m not following. What is this function for and what do you want to do with it?

If you’re using it in your template, all helpers can use Meteor.userId(), so I’m not sure what the issue is there. It’s also very easy to pass arguments to helpers in your template, like {{yourHelper argument}}, though you wouldn’t pass the userId that way, you’d just use Meteor.userId() in the helper function itself. If you want a function that basically acts as a wrapper for a long query, then I’d just pass the userId as an argument.

Here is an example of a collection helper you might want to use in methods, publish functions, template helpers, event handlers, and directly in html (it returns true if the user is authorized to edit the post):

Posts = new Meteor.Collection('posts');
Posts.helpers({
  ch_userCanEdit: function () {
    var userId = ...
    var canEdit = ... depends on userId ...
    return canEdit;
  }
});

Side note: helper arguments will remain painful until we get handlebars subexpressions. For example you cannot pass arguments in this case:

{{#each posts}}
  {{>sign green=this.ch_userCanEdit}}
{{/each}}

I also have a collection helper that checks for a user ID, using Meteor.userId(). The collection helper works as expected everywhere except publications. What would be a good approach here?

Did you see this one: https://github.com/peerlibrary/meteor-user-extra which seemingly allows to use Meteor.userId() everywhere?

1 Like

Thanks for the link. I wonder if the project is still maintained.

@mitar is still a very active developer and also he wants to have that kind of functionality in core: https://github.com/meteor/meteor/issues/5462

I would test it out for sure, my experience with his packages is just that they are great. He is also the main guy on the https://github.com/meteor/blaze project.

1 Like