meteor-collection-hooks icon indicating copy to clipboard operation
meteor-collection-hooks copied to clipboard

Bind arbitrary userId to database functions

Open turbobuilt opened this issue 9 years ago • 3 comments

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?

turbobuilt avatar Feb 26 '16 04:02 turbobuilt

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.

zeroasterisk avatar Aug 22 '16 23:08 zeroasterisk

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();
    });
}

turbobuilt avatar Aug 22 '16 23:08 turbobuilt

I'll try it out

zeroasterisk avatar Aug 22 '16 23:08 zeroasterisk