Meteor-Community-Packages / meteor-collection-hooks

Meteor Collection Hooks
https://atmospherejs.com/matb33/collection-hooks
MIT License
657 stars 92 forks source link

Bind arbitrary userId to database functions #178

Open turbobuilt opened 8 years ago

turbobuilt commented 8 years ago

Hello,

I'm trying to get these to work with api calls. I'm having a lot off trouble because the rest api doesn't use ddp and I can't get the user logged in. It's easy for me to get the user Id (that I am sure has been authenticated in some other way) from the api calls though. Is there a way I can bind a user id to these collection hooks without the user being logged in?

zeroasterisk commented 8 years ago

I have created an issue on simple rest and a test repo demonstrating this problem, I too am having.

The issue is the simple-rest faking the invocation context, find for the method, but not setting Meteor w/ variables/environment.

recommendations welcome.

turbobuilt commented 8 years ago

Hi @zeroasterisk,

@Stubailo showed me how to do this. Include the code below, then run a function as a certain user with the code below. You have to do auth yourself, but it gets the job done.

runAsUser = function(userId,func){
    const DDPCommon = Package['ddp-common'].DDPCommon;
    var invocation = new DDPCommon.MethodInvocation({
      isSimulation: false,
      userId: userId,
      setUserId: ()=>{},
      unblock: ()=>{},
      connection: {},
      randomSeed: Random.id()
    });

    return DDP._CurrentInvocation.withValue(invocation, () => {
      return func();
    });
}
zeroasterisk commented 8 years ago

I'll try it out