Guig
Guig

Reputation: 10197

Meteor - Set the logged in user

How can I set the logged in user directly ? Something like

Picker.route('sayhi', (params, req, res) => {
  if (req.method === 'POST') {
    const customUserId = getCustomUserId(req.headers);
    Meteor.setUserId(customUserId);
    console.log("hey I'm", Meteor.user());
    res.end('ok');
  }
});

Upvotes: 2

Views: 186

Answers (2)

Tomasz Lenarcik
Tomasz Lenarcik

Reputation: 4880

For what it's worth, this is the way it's internally done in meteor:

DDP._CurrentInvocation.withValue(new DDPCommon.MethodInvocation({
  isSimulation: false, userId: getCustomUserId(req.headers),
}), function () {
  // now things like Meteor.userId() work as expected
  // also every method called here will get the right userId
});

The only drawback of this solution is that it feels like using some kind of private API (or at least non-documented methods) that may potentially change over time. If you're not afraid of it, then I believe it's a way to go.

Edit

As @Guig mentioned in the comment, one will also need to meteor add ddp-common.

Upvotes: 2

Michel Floyd
Michel Floyd

Reputation: 20226

Are you trying to allow an admin to impersonate users? If so then the gwendall:impersonate package could be helpful to you.

Impersonate.do(userId, callback)

Or are you trying to have users authenticate in a different way? There are many existing packages for authenticating in a variety of ways.

Upvotes: 0

Related Questions