Choosing which fields of the current user appear on `this` in Methods/Pubs

Anyone interested in being able to choose which fields are tacked onto this in Meteor publications/methods?

Useful:

//in method or pub

if (!this.userId() || this.banned){
  throw new Meteor.Error('User not authorized.')
}

Of course, the problem is easy to solve. I could just write a query in-line. This is the classic cache versus compute when needed.

How often does Meteor query Mongo to assign a value to this.userId ? If it happens at login, that’s wonderful. We could hack that function and make it assign a few other properties to the publisher’s this, and just gracefully logout the client when those fields change on the server.

Any ideas? Any solutions already ? Any packages ?

Thank you!

I think packages can only emulate this kind of feature using queries (example here).

Last time I had a look at this.userId in Meteor code, I found it deeply linked with several Meteor features. However, I like your idea :- )

1 Like

Hey Steve, wow, thanks a ton for that link. The title alone is enough to help me out.

I’ve no idea why I didn’t just think of using Accounts.onLogin to instantiate a static cache on a per connection basis. This function is wonderful. I did an old console.log(arguments) and it gives you (user, connection) objects: perfect!. :smile: Especially considering I’m already caching some data per connection anyways…

Heh, I’m still not sure if I should trust security logic in my methods/pubs to get data from an in-memory cache… but I suppose, well, the data ultimately comes from Mongo since it is the user object.

Dang - Meteor is so fun. I love how you need a function and then boom it’s there, because MDG coded it up already.